Solved

issue returning a json Id variable

Posted on 2014-02-10
5
309 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 43

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 43

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 43

Expert Comment

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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why do people dis php? 5 45
PHP AJAX JSON 2 57
Scroll 5 news at a time. 4 25
jQuery for Each 12 16
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

815 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

8 Experts available now in Live!

Get 1:1 Help Now