PHP通过1个按钮和1个动作提交多个表单

时间:2013-10-16 02:30:02

标签: php

我想使用PHP提交包含1个按钮和1个操作目标的多个表单。有可能吗?

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'];

我尝试使用该代码,但它只显示$_POST['class']值。对于名称,它显示错误:Undefined index: name in...

请建议。

3 个答案:

答案 0 :(得分:0)

每个输入你不需要一个表格,你可以在一个表格中有一百万个......

<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>

应该没问题,你真的不需要提交js。更好地支持html提交输入

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

答案 1 :(得分:0)

您实际上想要在一个表单上使用两个字段 - 然后您可以毫无问题地提交这些字段:

<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>

或者如果你在JS代码中执行其他任务,你可以使用一些JS提交它。

答案 2 :(得分:0)

如果jquery是一个选项,那么.deferred可能就是你要找的东西。

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;
});
}

这是我前一段时间问这个问题的链接。 How to submit multiple jquery posts to a page one after another if database updates successfully?