syntax error in json using ajax and php -



syntax error in json using ajax and php -

here total code. error seems in jsontesting.php page variables literature, history , comedy.it's giving me these variables not defined.these variables supposed post ajax. please help asap.here php page (jsontesting.php):

<?php $library= '{"closets":[ {"id":"001","theme":"literature","shelves": { "books": [{"id":"b1","genre":"english literature"}, {"id":"b2","genre":"arabic literature"}, {"id":"b3","genre":"french literature"} ] } }, {"id":"002","theme":"comedy","shelves":{ "books":[{"id":"b11","genre":"english comedy"}, {"id":"b22","genre":"arabic comedy"}, {"id":"b33","genre":"french comedy"} ] } }, {"id":"003","theme":"history","shelves":{ "books":[{"id":"b111","genre":"english history"}, {"id":"b222","genre":"arabic history"}, {"id":"b333","genre":"french history"} ] } } ] }'; //isset used create sure reciving post ajax if(isset($_post["literature"])) $literature=$_post["literature"]; if(isset($_post["comedy"])) $comedy=$_post["comedy"]; if(isset($_post["history"])) $history=$_post["history"]; $library = json_decode($library, true); $literaturebooks = $library['closets'][0]['shelves']['books']; $comedybooks= $library['closets'][1]['shelves']['books']; $historybooks= $library['closets'][2]['shelves']['books']; if($literature) { echo json_encode($literaturebooks); exit(); } if ($comedy){ echo json_encode($comedybooks); exit(); } if($history){ echo json_encode($historybooks); exit(); } ?>

try , both similar achieved using jquery each coz 'each' handle both object , json array

$.ajax({ url:"jsontesting.php", data: {literature:'1'}, type:'post', success:function(data){ if(data) { $.each(data,function(key,value){ $("#books").append('<span>literature books are'+value.id+'</span><br/>'); } }, error:function(){ alert("error in connection"); } }); });

if not working .make sure json response jsontesting.php correct

json

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -