rixter
rixter

Reputation: 1301

Why does this jQuery .post fail to return a result, and how can I debug in Firebug?

This script used to work fine, so I am mystified as to why it's not working now; but it uses .post in a simple php call to retrieve a data record given two keys, mndx and sndx, both of which contain valid data.

 function fetchgenotype() {
    // here's where we use an Ajax function to fetch the allele values without reloading the page.
    // Get the index number of the genotype record to retrieve; retrieve it; populate the alleles.

    var mndx, sndx = 0;

    mndx = $('#marker').val();

    if (Study_selected) {
      sndx = $('#stylabid').val();
    } else { 
      sndx = $('#pjtlabid').val();
    }    

    // for debugging...    
    //alert('sndx='+sndx+', mndx='+mndx);

    // This is the cryptic jQuery 'magic' that fetches the genotype values when the
    // Fetch button is clicked, and displays the values fetched.

    $.post("fetchAlleles.php", {mndx: mndx, sndx: sndx},

      function(result) {

    // The .each method used on the return object is the key to displaying multiple
    // run dates, and genotype values, associated with a sample. 

    i=0;
    $.post("fetchAlleles.php", {'mndx': mndx, 'sndx': sndx},



function(result) {
    // this works for a single return value
    //$('#allele_1_1').get(0).value = result[0].allele1;
    //$('#allele_1_2').get(0).value = result[0].allele2;
    //$('#run_date1').get(0).value = result[0].run_date;

    // The .each method used on the return object is the key to displaying multiple
    // run dates, and genotype values, associated with a sample. 

    i=0;
$.each(result,function() {
  if (i==0) {
    $('#allele_1_1').val(this.allele1);
    $('#allele_1_2').val(this.allele2);
    $('#run_date1').val(this.run_date);
  } else if (i==1) {
    $('#allele_2_1').val(this.allele1);
    $('#allele_2_2').val(this.allele2);
    $('#run_date2').val(this.run_date);
  } else if (i==2) {
    $('#allele_3_1').val(this.allele1);
    $('#allele_3_2').val(this.allele2);
    $('#run_date3').val(this.run_date);
  }
  i=i+1;
    })
  }
);
      }

I have attempted to use Firebug to step into the code, but in the above code as soon as it steps past the .post line, it says result is undefined.

The relevant part of the php script is:

 $sql = "SELECT allele1, allele2, run_date FROM geno.genotypes WHERE markers_id=".$mndx." AND gsamples_id=".$sndx;

    // create array to hold results
    $obj_arr = array();
    $fetchresult = pg_exec($dbconnect, $sql);
    if ($fetchresult) {
        while ($obj = pg_fetch_object($fetchresult))
        // Add this object to our array of objects to return
        if (!array_push($obj_arr,$obj)) {
            echo("Error pushing results onto obj_arr");
            showerror(0,"Failed to connect to database",'fetchAlleles',38,"Failed in line 38");
            exit;
        }        
        // for debugging
        //print_r($obj_arr);
    } else {
        echo "(25) Failed to retrieve results.<BR>SQL: ".$sql."</br>";
    }

    // It appears necessary to return the array as json encapsulated.
    echo json_encode($obj_arr);

Any help much appreciated! --rixter

Upvotes: 0

Views: 290

Answers (2)

coolxeo
coolxeo

Reputation: 563

If you define the type of the post request as json you need to return json even for errors

You can debug it in your result function using

console.log(result);

Upvotes: 1

Sushanth --
Sushanth --

Reputation: 55750

Try this

$.post("fetchAlleles.php", {'mndx': mndx, 'sndx': sndx},

Encase your key's in stings and check if this works..

ALso check for any errors in the console section of the browser.. Check what the parameters in the Request section are , if this is successful check the response

Upvotes: 1

Related Questions