Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access JSON data returned from a jQuery .post?
    text
    copied!<p>I have a simple jQuery .post call that needs to process the returned data, and despite the numerous other similar posts, I cannot seem to get the syntax quite right.</p> <p>Here is my jquery call:</p> <pre><code>$.post("fetchAlleles.php", {mndx: mndx, sndx: sndx}, function(result) { $('#allele1').text(result[0]); $('#allele2').text(result[1]); }, "json"); </code></pre> <p>And here is the php code that is called:</p> <pre><code> $mndx = $_POST['mndx']; $sndx = $_POST['sndx']; $dbconnect = pg_pconnect("host=".$hostname." user=".$dbuser." dbname=".$dbname); if (!$dbconnect) { showerror(0,"Failed to connect to database",'fetchAlleles',16,"username=".$dbuser.", dbname=".$dbname); exit; } $sql = "SELECT allele1, allele2 FROM geno.genotypes WHERE markers_id=".$mndx." AND gsamples_id=".$sndx; $fetchresult = pg_exec($dbconnect, $sql); if ($fetchresult) { $arr = pg_fetch_array($fetchresult, 0, PGSQL_NUM); } else { echo "(25) Failed to retrieve results.&lt;BR&gt;SQL: ".$sql."&lt;/br&gt;"; } echo json_encode($arr); </code></pre> <p>Now, I know the php is working correctly, as I can see in Firebug that it returns the desired data. I just don't see how to access it using the success function. I want to assigned the return data to the value of the two elements id allele1 and allele2. </p> <p>I know I'm close, but haven't been able to find quite the right example, or decipher the jquery documentation. Any help will be much appreciated!</p> <p>--rixter</p> <p>EDIT 1: Here's the answer:</p> <p>$("#allele1").get(0).value = result[0]; $("#allele2").get(0).value = result[1];</p> <p>from Simanchal Pattanaik (found in another question)</p> <p>answered my question...thanks Simanchal!</p>
 

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