0

Following are my code i want to retrieve values from JSON which is returned by php file. following are my php code.

$i=1;
foreach($chck as $value){
$qry_a = "SELECT ans_tags FROM wp_pp_actionphp_answers where id=".$value['answer_id'];
$result_a = $wpdb->get_results( $qry_a );  
$final[]=array(
       "question_id_$i"=>$value['question_id'],
       "answer_id_$i"=>$value['answer_id'],
       "ans_tags_$i"=>$result_a[0]->ans_tags,
       "test_attempt_$i"=>$test_count_by_email,
       );
       $i++;
   }
$jsonstring = json_encode($final);
print_r($jsonstring);//Return JSON to javascript file
exit();

Following are my javascript code.

function get_result(result_id,email){
var data='result_id=' + result_id+"&email="+email;
            $.post(
            ajaxurl + '?action=actionphp_get_result',
            data,
            function(result){
                document.write(result);

            }
            );

}

following are my result.

[{"question_id_1":"2","answer_id_1":"3","ans_tags_1":"","test_attempt_1":"181"},{"question_id_2":"1","answer_id_2":"1","ans_tags_2":"This is a tag test","test_attempt_2":"181"}]

How can i retrive values.

vishal
  • 27
  • 8
  • You should probably search for this first (before posting): http://stackoverflow.com/questions/4935632/parse-json-in-javascript – Rob Bailey May 16 '16 at 15:04
  • Possible duplicate of [Safely turning a JSON string into an object](http://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object) – gre_gor Jun 04 '16 at 11:44

1 Answers1

1

You can simply parse the JSON in Javascript:

var data = "{\"a\": 1, \"b\": 2, \"c\": [1,2,3]}"; // your result
var obj = JSON.parse(data);

Then you can simply access the data stored in obj:

obj.a -> 1
obj.b -> 2
obj.c[0] -> 1

EDIT (thanks to Webomatik):

Of course you have to have to pay attention to your result. In your case your result is an array, so you could access the single objects in the array via

obj[0].question_id_1 // "2"
obj[0].answer_id_1 // "3"
Luca Schimweg
  • 747
  • 5
  • 18
  • 2
    Good answer. I woud just point out that result is an array of objects, so the correct access to the values would be obj[0].question_id_1, obj[0].answer_id_1, etc..... – Webomatik May 16 '16 at 15:53
  • 1
    Yeah, but that depends on his result, but you are right, in his use scenario it would be an array. – Luca Schimweg May 16 '16 at 15:54
  • Thanks @LucaSchimweg for your help. I am stuck into weird situation the code runs all good at localhost but as i deployed on server the array returns 0. updated php code: $final[]=array("test_attempt"=>$test_count_by_email); I have changed the js per your suggestion: function(result){ alert(result); var i=1,total=0; var obj = JSON.parse(result); var array_size=Object.keys(obj).length; alert(obj[i].question_id); } Could you please help me out. – vishal May 17 '16 at 13:36
  • 1
    Doesn't help much if you tell it like that, best would be if you open a new question and tell us about your versions, if the PHP runs on the same host the website is on, ... – Luca Schimweg May 17 '16 at 13:40
  • Live sever version PHP Version 5.5.9-1ubuntu4.16 localhost PHP Version 7.0.5 Since i am a new user am having limited access to post questions,request to please be with me on comment over this question. – vishal May 17 '16 at 13:47
  • @LucaSchimweg waiting for your response, would be great if you can help me out. Thanks – vishal May 17 '16 at 13:55
  • have you updated the url? You can't request to http://localhost if you deployed the script – Luca Schimweg May 17 '16 at 14:04
  • I am sorry, not able to understand what you mean by updating the url? – vishal May 17 '16 at 14:11
  • to what URL do you make the HTTP-Request in the Javascript? – Luca Schimweg May 17 '16 at 14:23
  • I have used $.post( ajaxurl + '?action=actionphp_get_result', data, function(result){ } and as suggested by you, I have changed the post URL and deployed on server, still it shows 0 while fetching array on server. – vishal May 17 '16 at 14:37
  • how is ajaxurl defined? – Luca Schimweg May 17 '16 at 14:54
  • 'ajaxurl' is path of the start file in wordpress. The code on start.php is: add_action( 'wp_ajax_actionphp_get_result', array($router, 'get_result')); and get_result() is a function which is calling perfectly by add_action. – vishal May 18 '16 at 04:06
  • Then i've got no idea – Luca Schimweg May 18 '16 at 04:14
  • Thanks @LucaSchimweg for your quick support. – vishal May 18 '16 at 04:27