简体   繁体   中英

ajax submit form why it cannot echo $_POST

I'm test using ajax submit form (submit to myself page "new1.php")

The thing that I want is, after click submit button, it will echo firstname and lastname. But I don't know why I do not see the firstname and lastname after submit.

here is new1.php page

<?php 
echo $_POST['firstname']."<br>";
echo $_POST['lastname']."<br>"; 
 ?>  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

</head>
<body>
    <form id="myform" action="new1.php" method="post">
        Firstname : <input type="text" name="firstname"> <br>
        Lastname : <input type="text" name="lastname"> <br>
        <input type="submit" value="Submit">
    </form>

        <script>
        // this is the id of the form
$("#myform").submit(function(e) {

    $.ajax({
           type: "POST",
           url: 'new1.php',
           data: $("#myform").serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert('yeah'); // show response from the php script.
           }
         });

    e.preventDefault(); // avoid to execute the actual submit of the form.
});
    </script>
</body>
</html>

In your case the best option to retrieve values as JSON format using json_encode in your PHP code and then accessing these values through data object.

Example:

PHP code:

if($_POST)
{
    $result['firstname'] = $_POST['firstname'];
    $result['lastname'] = $_POST['lastname'];

    echo json_encode($result);
    die(); // Use die here to stop processing the code further 
}

JS code:

$("#myform").submit(function (e) {

    $.ajax({
        type : "POST",
        url : 'new1.php',
        dataType : 'json', // Notice json here
        data : $("#myform").serialize(), // serializes the form's elements.
        success : function (data) {
            alert('yeah'); // show response from the php script.
            // make changed here
            $('input[name="firstname"]').text(data.firstname);
            $('input[name="lastname"]').text(data.lastname);
        }
    });

    e.preventDefault(); // avoid to execute the actual submit of the form.
});

When you use form as a serialize you have to retrieve like this.

Edit your ajax like this :

data: { formData: $("#myform").serialize()},

Then you can retrieve like this in your controller:

parse_str($_POST['formData'], $var);
echo "<pre>";
print_r($var);
exit;

Change from

alert('yeah'); // show response from the php script.

to

alert(data); // show response from the php script.

Make some changes in javascript here:

success: function(data)
       {
           $('#response').html(data); // show response from the php script.
       }

And in html code make a div with id response

<div id="response"></div>

the value firstname, lastname will not display because you called the new1.php via ajax and the data (firstname, lastname and the page code) is returned to java script variable (data) you need to inject the data to your document

Try this

$.ajax({
    type: "POST",
    url: 'new1.php',
    data: $("#myform").serialize(), // serializes the form's elements.
    success: function(data) {
        document.documentElement.innerHTML = data;
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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