user3733945
user3733945

Reputation: 31

javascript submit form from function

I have this code:

    var changedate  = document.getElementById("date");

    changedate.onchange = submitform;

    function submitform() {
    document.forms["form"].submit();

    }

However I have 3 submit buttons in my form, I want to call the submit button with ID 'save' with this function. How, can I do this, because at the moment when I change the value in 'date' the form gets submitted, but not in the way i manually push the submit button 'save' at the bottom of the form.

Upvotes: 1

Views: 2083

Answers (3)

user3733945
user3733945

Reputation: 31

I think the problem comes after submitting the form:

There is a part:

<?php
if (isset($_POST['save'])) {

// do something 
}
?>

This is not executed because I dont click on the submit button 'save' in the form.

Upvotes: 0

Govind Balaji
Govind Balaji

Reputation: 639

Try adding <input type="hidden" name="submitbutton"> . In each button, before submitting set the value of this hidden field accordingly. In the code executed when date is changed, set the hidden value as how you set for the save button Eg:

<form method="post" action="something.php">
    <input type="submit" value="save" onsubmit="setType('save');return true;"/>
    <input type="submit" value="btn2" onsubmit="setType('btn2');return true;"/>
    <input type="submit" value="btn3" onsubmit="setType('btn3');return true;"/>
    <input type="hidden" id="hdn" value=""/>
</form>

In your js,

function setType(msg){document.getElementById('hdn').value=msg;}

and when date is changed,

setType('save');
document.forms["form"].submit();

Make sure you catch correctly in your php/asp file.

Upvotes: 0

Uttam Kadam
Uttam Kadam

Reputation: 458

You can use :

document.getElementById("myForm").submit();

where myForm is id of your form.

Upvotes: 1

Related Questions