Solved

issue returning a json Id variable

Posted on 2014-02-10
5
307 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
  • 3
  • 2
5 Comments
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 500 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 42

Accepted Solution

by:
Chris Stanyon earned 500 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 42

Expert Comment

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 how to look for a specific file type in a local or remote server directory using PHP.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now