Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing Nested Objects in JSON (Key Pairs/MYSQL Array)
    primarykey
    data
    text
    <p>I have a mysql query create this output for my json data. I have not been able to figure out how to access the properties within this output however. </p> <p>I have tried using data.objA which returns undefined and objA][0] and am at a loss. Any assistance would be greatly appreciated.</p> <p>JSON</p> <pre><code>({"objA":"yes","objB":[{"username":"ah2122","client_password":"288c0e42ab41faef3d1015e6fc299644","client_id":"36"}]}) </code></pre> <p>PHP</p> <pre><code>&lt;?php include 'init.php'; //get the posted values $user_name=htmlspecialchars($_POST['client_username'],ENT_QUOTES); $pass=md5($_POST['client_password']); //now validating the username and password $sql="SELECT username, client_password, client_id FROM clients WHERE username='".$user_name."'"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { $rows[] = array( "username" =&gt; $row['username'], "client_password" =&gt; $row['client_password'], "client_id" =&gt; $row['client_id']); } //if username exists if(mysql_num_rows($result)&gt;0) { //compare the password if(strcmp($rows[0]['client_password'],$pass)==0) { $SUCCESS="yes"; } else $SUCCESS="no"; } else $SUCCESS="no"; //Invalid Login $rows2 = array( 'objA' =&gt; $SUCCESS, 'objB' =&gt; $rows ); $json = json_encode($rows2); $callback = $_GET['callback']; echo $callback.'('. $json . ')'; ?&gt; </code></pre> <p>SCRIPT</p> <pre><code>$(document).ready(function() { $("#client_login_form").submit(function() { //remove all the class add the messagebox classes and start fading $("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000); //check the username exists or not from ajax var cu = $("#client_username").val(); var cp = $("#client_password").val(); $.post("http://www.website.com/splash/cajax_login.php",{ client_username:cu,client_password:cp,rand:Math.random() } ,function(data) { if(data.objA=='yes') //if correct login detail { $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox { //add message and change the class of the box and start fading $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1, function() { window.localStorage["cusername"] = cu; window.localStorage["cpassword"] = cp; //redirect to secure page document.location="#client_home"; }); }); } else { $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox { //add message and change the class of the box and start fading $(this).html('Login Failed').addClass('messageboxerror').fadeTo(900,1); }); } }); return false; //not to post the form physically }); //now call the ajax also focus move from $("#client_password").blur(function() { $("#client_login_form").trigger('client_submit'); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload