Reputation: 1301
I'm new to jQuery and ajax, and so am trying to get this simple example to work; but so far no luck with using .post, php and a database call...
Here is my jQuery code:
function fetchgenotype() {
var mndx = document.EditGenotype.marker.options[document.EditGenotype.marker.selectedIndex].value;
var sndx = document.EditGenotype.labid.options[document.EditGenotype.labid.selectedIndex].value;
$.post("fetchAlleles.php", {mndx: mndx, sndx: sndx},
function(result) {
$('#results').append(result);
postToPage('#results');
});
}
function postToPage(arr) {
var allele1 = arr[0];
var allele2 = arr[1];
$('#allele1').text(allele1);
$('#allele2').text(allele2);
}
And here is what the essential part of fetchAlleles.php looks like:
$mndx = $_POST['mndx'];
$sndx = $_POST['sndx'];
// query the database.
$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 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.<BR>SQL: ".$sql."</br>";
}
return function($arr);
I now that my jQuery .post function is getting called, but nothing happens on the screen, and nothing shows up in my javascript error console. Any suggestions as to what's wrong?
TIA, --rixter
Upvotes: 0
Views: 99
Reputation: 71939
Problems I see:
Your PHP code outputs nothing when there is no error. I think you want something like echo json_encode($arr);
instead of return function($arr);
;
On the JS side, your postToPage
function is being passed a string (not an Array!). Then, inside the function, you try access this string by index (arr[0]
, arr[1]
). That will return a single character for each index -- probably not what you want.
Upvotes: 1