简体   繁体   中英

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.

You can use :

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

where myForm is id of your form.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM