0

I need to answer PHP processing with Ajax return But I do not know how to turn two variables

HTML

<html>
<head>
 <title>Sceener Ajax</title> 
 <script src="jquery.js"></script>
</head>
<body>


 <div id="name"></div>
 <div id="family"></div>



<script type="text/javascript">
$(document).ready(function(){
        $.ajax ({
            type: "POST",
            url: "ajax.php",
            success: function( result_1 ) {
                var name = result_1;
                $("#name").html(name);
            }
        });
});
</script>
</body>
</html>

ajax.php

<?php
$name = 'My Name';
$family = 'My Family';
echo ( $name );
?>

How can I do this please help me

2
  • I tried but did not succeed json Commented Feb 13, 2017 at 15:33
  • json_encode( array( 'name' => $name, 'family' => $family) ) Commented Feb 13, 2017 at 15:34

4 Answers 4

2

You should return a json object instead of a string.

In PHP, create an array with the values and create a json string from it:

$response = [
    'name' => 'My Name',
    'family' => 'My Family'
];

// Encode the array as a json string
echo json_encode($response);

Add dataType: 'json' to the ajax call, to get jQuery to parse the response as a json object and then fetch the values from it:

$(document).ready(function(){
    $.ajax ({
        type: "POST",
        url: "ajax.php",
        dataType: 'json', // <-- This will make jQuery handle the json response correctly
        success: function( response ) {
            // Now you can get both values from the json object
            console.log(response.name);
            console.log(response.family);
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

2

Put all the values you want to return in an array, encode it to JSON, and read it back in your javascript:

So the PHP becomes

<?php
    $name = 'My Name';
    $family = 'My Family';
    $result = array ('name' => $name, 'family' => $family);
    echo ( json_encode($name ));
?>

And your javascript like this:

<script type="text/javascript">
$(document).ready(function(){
        $.ajax ({
            type: "POST",
            url: "ajax.php",
            success: function( result ) {
                var name = result.name;
                var family = result.family;
                $("#name").html(name);
            }
        });
});
</script>

Comments

1

In your PHP create an array or an object to contain all your data

<?php
$reply['name'] = 'My Name';
$reply['family' = 'My Family';
// this will convert the array to a JSON String 
// for transmission to the browser
echo json_encode($reply);
?>

In your javascript code to expect an object to be returned

<script type="text/javascript">
$(document).ready(function(){
    $.ajax ({
            type: "POST",
            url: "ajax.php",
            dataType = 'json',      // tell jquery to expect a JSON String
                                    //and auto convert to a js object
            success: function( data ) {
                $("#name").val(data.name);
                $("#family").val(data.family);

            }
        });
});
</script>

2 Comments

not work ! console error is : Uncaught SyntaxError: Invalid shorthand property initializer
Do you have something on your page with an id="family"
0
<?php $data['name']= 'My Name'; 
$data['family'] = 'My Family'; 
echo json_encode($data);
?>

You got answer in json format.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.