1

我有以下 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 脚本,它在提交时从输入中接收 $_POST 值,并运行一个条件来测试单击了哪个提交按钮。

像这样:

$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);但这也不能解决我的问题。

提前感谢您的帮助。

4

3 回答 3

2

您忘记了单引号和)输入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>
于 2013-03-08T04:02:12.890 回答
0

Simplest answer would be:

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

Of course, this won't give exactly what you're looking for.

You can also do something like this:

<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'
});
于 2013-03-08T04:14:37.953 回答
-1

将按钮更改为此(更改 w/您的表单名称)..

<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

于 2013-03-08T04:01:00.983 回答