0

I'm new to jQuery, and have not been able to debug this ajax call in Firebug:

This is my ajax call:

var styndx = $('#studylist option:selected').val();
var studyname = $('#edit_field').val();
            
    
$.post("saveStudyName.php", {'type': 'update', 'studyname':studyname, 'styndx':styndx},
    function(resultmsg) {
    $('#edit_field').val('');
    $('#savebtn').attr('disabled',true);
    refresh_studynames();
});

And this is the function refresh_studynames:

function refresh_studynames()
{
  $.ajax({                                      
     url: 'getStudyNames.php',                  
     data: "",                                                             
     dataType: 'json',               
          error: function() {
            alert('Refresh of study names failed.');
          },
     success: function(data)
     {
        $data.each(data, function(val, sname) {
        $('#studylist').append( $('<option></option>').val(val).html(sname) )
      });
     } 
  });
}

Finally, this is the php script getStudyNames.php ($dbname,$dbconnect, $hostname are all populated, and $dbconnect works; the backend database is Postgres, and pg_fetch_all is a Postgres function in PHP that returns result as an array):

$dbconnect = pg_pconnect("host=".$hostname." user=".$dbuser." dbname=".$dbname);    
    if (!$dbconnect)    {
        showerror(0,"Failed to connect to database",'saveStudyName',30,"username=".$dbuser.", dbname=".$dbname);
        exit;
    }
    
    $sql = "SELECT ST.studyindex,ST.studyabrv AS studyname
            FROM ibg_studies ST
            ORDER BY studyname";
    

    $fetchresult = pg_exec($dbconnect, $sql);
    if ($fetchresult) {
        $array = pg_fetch_all($fetchresult);
        echo json_encode($array);
    } else {
        $msg = "Failure! SQL="+$sql;
        echo $msg;
    }

Any help much appreciated....

5
  • 1
    Do you see any JSON content when you browse directly to getStudyNames.php? Commented Dec 7, 2012 at 0:54
  • The line ` $('#studylist').append( $('<option></option>').val(val).html(sname) )` looks wrong. Why val(val)? Where is #studylist emptied? Commented Dec 7, 2012 at 1:11
  • Try: ` var $studylist = $('#studylist').empty(); $data.each(data, function(i, record) { $studylist.append( $('<option/>').html(record.sname) ); }); ` Commented Dec 7, 2012 at 1:15
  • @Beetroot-Beetroot Put it in an answer Commented Dec 7, 2012 at 2:00
  • yes, I do get json output when I browse to getStudyNames.php; good tip! Commented Dec 7, 2012 at 21:55

1 Answer 1

1

The line

$('#studylist').append( $('<option></option>').val(val).html(sname) );

looks wrong.

I'm not too sure but you could try :

var $studylist = $('#studylist').empty();
$data.each(data, function(i, record) {
    $studylist.append( $('<option/>').html(record.sname) );
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help, Beetroot; will try this code as soon as debug saveStudyName.php, because it is not updating the record, and I do not see how I can debug a .post ajax call....
This helped, but is not an answer that seems to work; am going to start a new post to address this; the other tips were helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.