PHP使用jQuery .post()提交带有多个提交按钮的表单 [英] PHP submitting a form with multiple submit buttons using jQuery .post()

查看:90
本文介绍了PHP使用jQuery .post()提交带有多个提交按钮的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML代码:

I have the following HTML code:

<form method="post" action="the_file.php" id="the-form">
    <input type="text" name="the_input" value="<?php if ( isset( $_POST['the_input'] ) echo $_POST['the_input]; ?>">
    <button type="submit" name="eat_something" value="TRUE">Eating</button>
    <button type="submit" name="eat_something" value="FALSE">Don't Eat</button>
</form>
<textarea id="result"></textarea>

此JS后面紧随其后:

$('#the-form').bind('submit', submitForm);
function submitForm(evt) {
    jQuery.post(
        $(this).attr('action'),
        $(this).serialize(),
        function(data) {
            $('#result').empty().append(data).slideDown();
        });
    evt.preventDefault();
}

我还有一个PHP脚本,可以从提交时的输入中接收$ _POST值,并运行条件以测试单击了哪个提交按钮.

I also have a PHP script that receives the $_POST value from the input on submit and runs a conditions to test which submit button was clicked.

赞:

$input = $_POST['the_input'];
$eating = $_POST['eat_something'];
if ( $eating == 'TRUE' ) {
    // Do some eating...
} else {
    // Don't you dare...
}

如果我不使用jQuery.post()函数,则会发布按钮的提交值.但是,由于某种原因,我无法使用jQuery.post()函数将按钮值传递给PHP $ _POST.如果我不使用jQuery.post(),则输出不会附加到textarea上,而是以文本格式附加在单独的页面上,例如文档.我也尝试过在按钮$('button[type=submit]').bind('submit', submitForm);上调用Submit函数,但这也不能解决我的问题.

If I don't use the jQuery.post() function the submit values from the button are posted. However, for some reason, I can't manage to pass the button value to PHP $_POST with the jQuery.post() function. If I don't use jQuery.post() the output doesn't get appended to the textarea but rather in a text format on a separate page, like a document. I've also tried calling the submit function on the button $('button[type=submit]').bind('submit', submitForm); but this doesn't solve my problem either.

预先感谢您的帮助.

推荐答案

您忘记了the_name输入中的符号引号和).

You forget signle quote and ) in the_name input.

<input type="text" name="the_input" value="<?php if (isset($_POST['the_input'])) { echo $_POST['the_input'] ; } ?>">

要获取按下表单按钮的值,您需要手动添加该值以进行序列化.

and for getting form button pressed value you need to append it value manually to serialize.

$form.serialize() + "&submit="+ $('button').attr("value")

示例

<script type="text/javascript">
    $(function()
    {
        $('button[type="submit"]').on('click',function(e)
        {
            e.preventDefault();
            var submit_value = $(this).val();
            jQuery.post
            (
                $(this).attr('action'),
                $(this).serialize()+ "&submit="+ submit_value,
                function(data)
                {
                    $('#result').empty().append(data).slideDown();
                }
            );
        });
    });
</script>

完整的测试代码

<?php
    if(isset($_POST['the_input']))
    {
        $input = $_POST['the_input'];
        $eating = $_POST['eat_something'];
        exit;
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <title>StackOverFlow</title>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">
        $(function()
        {
            $('button[type="submit"]').on('click',function(e)
            {
                e.preventDefault();
                var submit_value = $(this).val();
                jQuery.post
                (
                    $('#the-form').attr('action'),
                    $('#the-form').serialize()+ "&eat_something="+ submit_value,
                    function(data)
                    {
                        $('#result').empty().append(data).slideDown();
                    }
                );
            });
        });
    </script>
</head>
<body>
    <form method="post" action="random.php" id="the-form">
        <input type="text" name="the_input" value="<?php if (isset($_POST['the_input'])) { echo $_POST['the_input'] ; } ?>">
        <button type="submit" name="eat_something" value="TRUE">Eating</button>
        <button type="submit" name="eat_something" value="FALSE">Don't Eat</button>
    </form>
    <textarea id="result"></textarea>
</body>
</html>

这篇关于PHP使用jQuery .post()提交带有多个提交按钮的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆