[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

issue returning a json Id variable

Posted on 2014-02-10
5
Medium Priority
?
313 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 44

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 44

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 44

Expert Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article discusses how to implement server side field validation and display customized error messages to the client.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

649 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