php - Ajax and Json query not working -
im having problem retieving info using ajax. tell me wrong code? have feeling has fact im using json , ajax in same function?
jquery
function updateimage() { var knownid = document.location.hash.substring(1); // remove # $.ajax({ //make ajax request type: "post", url: "testimagelook.php", //file name data: {boxid: knownid}, success: function(server_response){ $.getjson("testimagelook.php", function(data) { var id = data.id; document.location.hash = id; known_images[id] = []; known_images[id] ['name'] = data.name; known_images[id] ['average'] = data.average; known_images[id] ['votes'] = data.votes; known_images[id] ['username'] = data.username; known_images[id] ['userid'] = data.userid; }); } }); $('#design').attr('src','img/boxes/'+knownid+'.png'); $("#lblname").html(known_images[knownid]['name']); $('#lblrating').html(known_images[knownid] ['average'] + " (" + known_images[knownid] ['votes'] + ") (<a href='user.php?uid=" + known_images[knownid] ['userid'] + "'>" + known_images[knownid] ['username'] + "</a>)"); }
my php file ajax connects follows:
php
<?php include('config.php'); if (isset($_post['boxid'])) { $knownid = $_post['boxid']; $query = mysql_query("select b.id boxid, b.name, b.date_added, u.username, u.id userid, u.active, b.active, b.active_admin, b.average average, b.votes votes boxes b, users u b.user_id = u.id , b.id >= $knownid limit 0,1"); $userdata = mysql_fetch_array($query, mysql_assoc); echo json_encode(array("id" => $userdata['boxid'], "name" => $userdata['name'], "average" => $userdata['average'], "votes" => $userdata['votes'], "username" => $userdata['username'], "userid" => $userdata['userid'])); } ?>
you don't need 2 requests. specifying datatype: 'json'
in first request tells jquery parse response json.
function updateimage() { var knownid = document.location.hash.substring(1); // remove # $.ajax({ //make ajax request type: "post", url: "testimagelook.php", //file name data: {boxid: knownid}, datatype: 'json', success: function(data){ var id = data.id; document.location.hash = id; known_images[id] = data; $("#lblname").html(data.name); $('#lblrating').html(data.average + " (" + data.votes + ") (<a href='user.php?uid=" + data.userid + "'>" + data.username + "</a>)"); } }); $('#design').attr('src','img/boxes/'+knownid+'.png'); }
php javascript jquery ajax
No comments:
Post a Comment