2

I'm new to JSON and AJAX, and as such have searched for solutions and experimented for a few days before resorting to asking here. I am using AJAX to process a PHP page on submit. It is saving the information fine, but I also need the PHP page to pass back the inserted ID. Here is what I have so far.

In the success:

success: function(){ 
 $('#popup_name img').remove();
 $('#popup_name').html('Saved'); 
 $('#fade , .popup_block').delay(2000).fadeOut(function() {
 $('#fade, a.close').remove(); //fade them both out
 $.getJSON(pathName, function(json){
 alert('You are here');
 alert("Json ID: " + json.id);
 });
 });
}

Then, the PHP script calls this method to insert the info and return the inserted id:

 public static function doInsertQuery($sparamQuery="",$bparamAutoIncrement=true,$sparamDb="",$sparamTable=""){
//do the insert
$iReturn = 0;
$result = DbUtil::doQuery($sparamQuery);
if(!$result){
 $iReturn = 0;
}
elseif(!$bparamAutoIncrement){
 $iReturn = DbUtil::getInsertedId();
}
else{
 $iReturn = DbUtil::getInsertedId();
}
//log the insert action
//if not a client logged in- cannot log to client db
if(Session::get_CurrentClientId() > 0){
 if($sparamTable != LogLogin::table_LOGLOGINS()){
 $oLog = new LogDbRequest();
 $oLog->set_Type(LogDbRequest::enumTypeInsert);
 $oLog->set_Request($sparamQuery);
 $oLog->set_RowId($iReturn);
 $oLog->set_TableName($sparamTable);
 $oLog->set_Before("NULL");
 $oLog->set_After(serialize(DbUtil::getRowCurrentValue($sparamDb,$sparamTable)));
 $oLog->insertorupdate_LogDbRequest();
 }
}
echo json_encode($iReturn);
return $iReturn;
}

I hope this makes sense. I'm at a complete loss here. Any help at all would be greatly appreciated! ~Mike~

asked Sep 1, 2011 at 17:10
8
  • 1
    echo json_encode(array("id"=>$iReturn)); Commented Sep 1, 2011 at 17:13
  • 1
    What does the $iReturn contain? Does it contain array('id' => 123);? Commented Sep 1, 2011 at 17:14
  • 2
    I don't think you need the $.getJSON() in the success callback. This would make another call to the server. Just output the ID as json in your initial result page. Commented Sep 1, 2011 at 17:52
  • i'm starting to wonder if the problem isn't just where i'm trying to echo the id in the actual php. Zeiss, i don't understand how to do what you're saying. Commented Sep 1, 2011 at 18:38
  • you success() function should take a parameter like this: success: function(data) { ... }. data should now return the output from your php request (if it is json, it get parsed). Also remove the $.getJSON()... call, since it makes another request to the server. Commented Sep 1, 2011 at 19:04

1 Answer 1

1

It's simple really. The success function accepts an argument corresponding to the response from the server.

Client side:

$.ajax({
 'url':'/path/to/script.php',
 'dataType':'json',
 'success':function(response){ //note the response argument
 alert(response.id); //will alert the id
 }
});

Server side:

<?php
 //...previous stuff here...
 $response = array('id' => $id); //where $id is the id to return to the client
 header('Content-type: application/json'); //Set the right content-type header
 echo json_encode($response); //Output array as JSON
?>
answered Sep 9, 2011 at 19:08
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.