Jquery and $.ajax, how to retrieve data from POST...

Posted on 2011-10-20
Medium Priority
Last Modified: 2012-05-12
Hey Guys and Gals,
I am finally stepping into the world of Ajax and I have a question....  How do I grab values that are returned from a script the javascript POSTed to?

This is what I am trying to do:

I have a 'Promo Code' field and want to check to see if the promo code is valid.  The javascript (JQuery) will post to my script and then send some data back (example: the Promotion Name that is tied to the promo code).

Here is some generic code, check the commented code for my issues...
  type: "POST",
  url: "some.php",
  data: "name=John&location=Boston",
}).done(function( msg ) { //---- It looks like 'msg' is the returned value from the some.php script.
  alert( "Data Saved: " + msg );

//How do I send 'msg' from the some.php file back to my requesting page?  Should 'msg' be a javascript variable in some.php?

Open in new window

Thanks for the help!
Question by:Slim81
  • 2
  • 2
LVL 13

Assisted Solution

haloexpertsexchange earned 2000 total points
ID: 37000746
msg should contain any output that came from the some.php page.
The output would be anything that you would echo out from the page. Basically anything that you would be sending to the browser for display.

Author Comment

ID: 37000817
Thanks for the reply.

Anything that the browser would display?

In the example I provided (pulled from the JQuery website), they use the variable/parameter of 'msg' in the '.done' function.  Do I have to reference or create 'msg' on the some.php file or am I free to name that variable/parameter whatever I want?

LVL 13

Accepted Solution

haloexpertsexchange earned 2000 total points
ID: 37000871
you do not need to reference or create msg on the php file.
So you what you will normally be getting back is html or possibly javascript output which you can then manipulate as you want.
I have used ajax calls to give me whole tables of html output and I have also used it to return to me a customized javascript function. Basically you can send back anything and don't need to reference any special variable on the server side.
But that also means don't echo anything out on that page except for what you want to be returned because you will get everything.

Author Comment

ID: 37000889
Brilliant.  JQuery has made ajax easy for me to understand.

ajax rocks.

Thanks for your help!

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

829 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