Search code examples
phpjquerypostsubmit-button

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


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>

Followed by this 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();
}

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.

Like this:

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

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.

Thanks in advance for you help.


Solution

  • 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")
    

    Example

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

    Complete Tested Code

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