?
Solved

issue returning a json Id variable

Posted on 2014-02-10
5
Medium Priority
?
312 Views
Last Modified: 2014-02-10
I am guessing this is going to be a huge oversight on my part but i have an issue with json that i havent been able to resolve..

i am using the following code to run an update/insert query

$.post($("#updateEnquiryform").attr("action"),$("#updateEnquiryform").serialize(),function(){
	
}).success(function(response){
	alert(response);
	alert(response.lastId);
},"json");

Open in new window


my php statement returns a response with the following :

$result->execute();
$insertedID = $pdo->lastInsertId();
$returnUrl = array ("lastId"=>$insertedID);
echo json_encode($returnUrl);

Open in new window



Essentially, all that i want to do is to return the last inserted ID whenever i have a new record. at the moment, when i alert the response, i get {"lastId":"0"}.. but when i try response.lastId it returns undefined ?

I'm not sure where i'm going wrong or if i'm even doing this with the right approach ?

Thanks
0
Comment
Question by:maddisoncr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 2000 total points
ID: 39847214
You're defining the json datatype in the wrong place. It's part of the $.post() call, not the success() call. Also, is there any reason why you define an empty success handler, only to attached another one. If you're going to use the success method, then just past null as the success handler to post()

$.post( $("#updateEnquiryform").attr("action"), $("#updateEnquiryform").serialize(), null, 'json')
.success(function(response){
	alert(response);
	alert(response.lastId);
});

Open in new window

0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 2000 total points
ID: 39847221
If you want to handle the success inline rather than by calling the success method, then this is what you'd do:
$.post(
   $("#updateEnquiryform").attr("action"),
   $("#updateEnquiryform").serialize(),
   function(response) {
      alert(response);
      alert(response.lastId);
   },
   'json'
);

Open in new window

0
 

Author Comment

by:maddisoncr
ID: 39847266
thanks chris, you're absolutely right..

i'd been hacking around and hadn't even noticed the first function.

all working as it should now

thanks again
0
 

Author Closing Comment

by:maddisoncr
ID: 39847267
excellent
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39847268
No worries - sometimes just needs a fresh pair of eyes :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question