Solved

issue returning a json Id variable

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Worldmap 1 28
What is define("__APPROOT__", __DIR__); 6 32
HTML editor custom button 3 59
AJAx/JQuery/JSON Error 3 13
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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)
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…

726 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