简体   繁体   中英

PHP submit Multiple Forms with 1 button and 1 action

I want to submit Multiple Forms with 1 button and 1 action target using PHP. Is is possible ?

HTML

<form name="myform" action="test_post.php" method="post">
Name: <input type='text' name='name' />
</form>

<form name="myform2" action="test_post.php" method="post">
Class: <input type='text' name='class' />
</form>

<a href="javascript: submitform()">Search</a>

JS

function submitform()
{
document.myform.submit();
document.myform2.submit();
}

PHP (test_post.php)

echo $name = $_POST['name'];
echo $class = $_POST['class'];

I tried with that code but it just show $_POST['class'] value. For name it show error : Undefined index: name in...

Please advice.

you dont need one form per input, you can have a million in one one form so ..

<form name="myform" action="test_post.php" method="post">
Name: <input type='text' name='name' />

Class: <input type='text' name='class' />
</form>

<a href="javascript: submitform()">Search</a>

should be fine, and you don't really need js to submit. a html submit input is better supported

<input id="submit" type="submit" value="submit">

You actually want two fields on one form - which you can then submit with no problems:

<form name="myform" action="test_post.php" method="post">
Name: <input type='text' name='name' />
Class: <input type='text' name='class' />
<input type='submit'>
</form>

Or you can submit it by using some JS anyhow if you do other thigns in the JS code.

if jquery is an option, then .deferred might be what you are looking for.

function submitform(){
//define a variable where we will store deferred objects
var def = true;

$("form").each(function() {

    var postResult = $.Deferred();

    //.when takes a deferred object as a param. 
    // if we don't pass a deferred object .when treats it as resolved.
    // as our initial value of def=true, the first .when starts immediately
    $.when(def).then(function(){
        $.post('post_destination.php', form.serialize()).done(function(){
            //the chain will fail after the first failed post request
            //if you want all the requests to complete in any case change the above .done to .always
            post.resolve();
        });
    });

    // now we reassign def with the deferred object for the next post request
    def = postResult;
});
}

and here's the link to when I asked the question some time ago. How to submit multiple jquery posts to a page one after another if database updates successfully?

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