krrr25
krrr25

Reputation: 679

How to get the response data in ajax call

I have a php file(index.php) with two fields when i post that form, in javascript i am doing form data serialize and send it to next php page(results.php) through ajax. When i try to print the data inside success it is not printing. FInd the below code.

<html>
<head>
    <title></title>
    <script src="../scripts/jquery-1.9.1.js"></script>
</head>
<body>
    <form method="post" name="index" id="indexform">
        <table border="1">
            <tr>
                <td>Name:</td>
                <td><input type="text" name="fname"></td>
            </tr>
            <tr>
                <td>Email:</td>
                <td><input type="text" name="email"></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" name="sendData"></td>
            </tr>
        </table>
    </form>
</body>
<script type="text/javascript">
    $( "#indexform" ).on( "submit", function( event ) {
          event.preventDefault();
          console.log( $(this).serialize() );
          var formdata = $(this).serialize();
         // alert(formdata);
          $.ajax({
                type:"POST",
                url:"result.php",
                dataType:'json',
                data:formdata,
                success: function(data){
                    alert(data);
                }
          });
        });
    </script>

In the above i cant print the data inside the success callback.

Upvotes: 3

Views: 16379

Answers (4)

Padmanathan J
Padmanathan J

Reputation: 4620

Try this

<script type="text/javascript">
        $( "#indexform" ).on( "submit", function( event ) {
              event.preventDefault();
              console.log( $(this).serialize() );
              var formdata = $(this).serialize();
              $.ajax({
                    type:"POST",
                    url:"result.php",
                    data:formdata,
                    success: function(html){
                        alert(html);
                    }
              });
            });
        </script>

In your result.php page

 $name=$_REQUEST['fname'];
    $email=$_REQUEST['email'];
    echo $name." ".$email;

Upvotes: 4

Renish Khunt
Renish Khunt

Reputation: 5824

Hello My Friends your html code is in

<script type="text/javascript">
    $( "#indexform" ).on( "submit", function( event ) {
          event.preventDefault();
          console.log( $(this).serialize() );
          var formdata = $(this).serialize();
         // alert(formdata);
          $.ajax({
                type:"POST",
                url:"result.php",
                dataType:'json',
                data:formdata,
                success: function(data){
                    alert(data);
                }
          });
        });
    </script>

and you result.php file code is in

<?php
    include 'db.php';
    $name=$_REQUEST['fname'];
    $email=$_REQUEST['email'];
    echo $name." ".$email;
?>

now the result.php file is return the entered name and email address.

Upvotes: 0

Syam Kumar
Syam Kumar

Reputation: 601

Echo the content inside results.php page.

Ex:

echo "two fields $first_field , $second_field  successfully inserted. " ;

This content results as an ajax response and stores in the data variable .

Upvotes: 0

jeroen
jeroen

Reputation: 91734

You are setting the dataType to json so you must ensure that you only return valid json.

That means you cannot echo or print whatever you want; you should collect your data in an array or object and then only once output that like:

echo json_encode($your_data);

Upvotes: 2

Related Questions