PHP not Sending JSON Object to JavaScript -
<?php header("content-type: application/json"); if(isset($_post['limit'])){ $limit = preg_replace('#[^0-9]#', '', $_post['limit']); require_once("connect_db.php"); $i = 0; $jsondata = '{'; $sqlstring = "select * tablename order rand() limit $limit"; $query = mysqli_query($con, $sqlstring) or die (mysqli_error()); while ($row = mysqli_fetch_array($query)) { $i++; $id = $row["id"]; $title = $row["title"]; $cd = $row["creationdate"]; $cd = strftime("%b %d, %y", strtotime($cd)); $jsondata .= '"article'.$i.'":{ "id":"'.$id.'","title":"'.$title.'", "cd":"'.$cd.'" },'; } $now = getdate(); $timestamp = $now[0]; $jsondata .= '"arbitrary":{"itemcount":'.$i.', "returntime":"'.$timestamp.'"}'; $jsondata .= '}'; echo $jsondata; } ?> <!doctype html> <html> <head> <script type="text/javascript"> var mytimer; function ajax_json_data(){ var databox = document.getelementbyid("databox"); var arbitrarybox = document.getelementbyid("arbitrarybox"); var hr = new xmlhttprequest(); hr.open("post", "json_mysql_data.php", true); hr.setrequestheader("content-type", "application/x-www-form-urlencoded"); hr.onreadystatechange = function() { if(hr.readystate == 4 && hr.status == 200) { var d = json.parse(hr.responsetext); arbitrarybox.innerhtml = d.arbitrary.returntime; databox.innerhtml = ""; for(var o in d){ if(d[o].title){ databox.innerhtml += '<p><a href="page.php?id='+d[o].id+'">'+d[o].title+'</a><br>'; databox.innerhtml += ''+d[o].cd+'</p>'; } } } } hr.send("limit=4"); databox.innerhtml = "requesting..."; mytimer = settimeout('ajax_json_data()',6000); } </script> </head> <body> <h2>timed json info request random items script</h2> <div id="databox"></div> <div id="arbitrarybox"></div> <script type="text/javascript">ajax_json_data();</script> </body> </html>
php code goes on separate file called "json_mysql_data.php". i'm next tutorial https://www.youtube.com/watch?v=-bv8p5oqnfw , runs fine him not me. tested "connect_db.php" mysql lone , works fine. seems me php doesn't go pass if (isset ($_post['limit'])) why...on html file "requesting..." message javascript code means waiting php. help guys.
you check ready state , alter content of databox response json inside onreadystatechange function:
hr.onreadystatechange = function(aevt) { if(hr.readystate == 4 && hr.status == 200) { … databox.innerhtml += …; … } … databox.innerhtml = "requesting..."; … } but alter html of databox:
databox.innerhtml = "requesting..."; still inside block of onreadystatechange function , after receive response, databox "requesting..." no matter receive. have move part prints "requesting..." outside of it:
hr.onreadystatechange = function(aevt) { if(hr.readystate == 4 && hr.status == 200) { … databox.innerhtml += …; … } … } … databox.innerhtml = "requesting..."; … update:
also, seems function ins't defined correctly, can see, 1 on mdn reference pages illustration receives parameter:
req.onreadystatechange = function(aevt) { … } but yours doesn't have such parameter:
hr.onreadystatechange = function() { … } ans that's it.
javascript php mysql json
No comments:
Post a Comment