2

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>
doflamingo
  • 567
  • 5
  • 23
  • 1
    fistname or firstname ? Check what your element name and what you are trying to echo – Rahul Jan 11 '17 at 11:35
  • You need to check in the console. Your code works fine. – Ionut Necula Jan 11 '17 at 11:35
  • Nothing error in console – doflamingo Jan 11 '17 at 11:37
  • You will not be able to see the firstname and lastname as its been firstly parsed when you page is loaded. If you need to show it you need to have an hidden field in form and after success of form stick the value to hidden variable in js – Nishant Nair Jan 11 '17 at 11:39
  • Pleaes go `inspect elements` the go to `network` here you can see the new1.php page name left hand side click and see the echo may found over there. – Pranav MS Jan 11 '17 at 11:42

5 Answers5

3

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.
});
Kalimah
  • 11,217
  • 11
  • 43
  • 80
2

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;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mr world wide
  • 4,696
  • 7
  • 43
  • 97
1

Change from

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

to

alert(data); // show response from the php script.
dhruv jadia
  • 1,684
  • 2
  • 15
  • 28
Gagandeep Gambhir
  • 4,225
  • 1
  • 29
  • 34
1

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>
Zain Farooq
  • 2,956
  • 3
  • 20
  • 42
1

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;
    }
});
Super User
  • 9,448
  • 3
  • 31
  • 47
Sajan
  • 813
  • 9
  • 14