returning multiple values using php to jquery.ajax with json

ajax, jquery, json, php

Solution

you can send multiple data in an array and then use json_encode

$output =  array('first'=>'Steven',
                 'last'=>'Spielberg',
                 'address'=>'1234 Unlisted Drive');

echo json_encode($output,JSON_FORCE_OBJECT);

and on other side you can access the value by this way

 success : function(resp) {(
              alert(resp.first);
              alert(resp.last);
              alert(resp.address);
            });

Problem

i'm trying to make a simple call to data from database using php and ajax. I need multiple results. hence i'm using json method. But its not working. ``` $.ajax({ type: "POST", data: "qid=162", url: "activity_ajax.php", dataType: json, success: function (data) { alert(data.first); } }); ``` My activity_ajax.php page returns the following ``` echo "first":"Steven","last":"Spielberg","address":"1234 Unlisted Drive"; ```

Original source