繁体   English   中英

PHP使用jQuery .post()提交具有多个提交按钮的表单

[英]PHP submitting a form with multiple submit buttons using jQuery .post()

我有以下HTML代码:

<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脚本,该脚本从Submit上的输入接收$ _POST值,并运行条件以测试单击了哪个Submit按钮。

像这样:

$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函数$('button[type=submit]').bind('submit', submitForm); 但这也不能解决我的问题。

在此先感谢您的帮助。

您会忘记the_name输入中的引号和)

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

为了获得按下表单按钮的值,您需要手动添加该值以进行序列化。

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

最简单的答案是:

<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']; ?>">
    <input type="submit" name="eat_something" value="Eating" />
    <input type="submit" name="eat_something" value="Don't Eat" />
</form>

当然,这并不能完全满足您的需求。

您还可以执行以下操作:

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

$('#the-form button').bind('click', function(){
    jQuery.post($('#the-form').attr('action'),
    {
        the_input: $('#theInput').val(),
        eat_something: $(this).attr('data-value')
    },
    function(data) { $('#result').empty().append(data).slideDown() },
    'json'
});

将按钮更改为此(更改您的表单名称)。

<input type="hidden" name="eat_something" value="" />
<input type="button" onclick="$('input[name=eat_something]').val('TRUE')" />
<input type="button" onclick="$('input[name=eat_something]').val('FALSE')" />

Javascript函数和PHP都很好。

谢谢!

@leo

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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