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

Posted on 2011-10-20
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
    LVL 13

    Assisted Solution

    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.
    LVL 4

    Author Comment

    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

    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.
    LVL 4

    Author Comment

    Brilliant.  JQuery has made ajax easy for me to understand.

    ajax rocks.

    Thanks for your help!

    Featured Post

    Hire Freelancers to Complete JavaScript Projects

    Source the talented Expert Exchange community
    for top quality work on your JavaScript projects.

    Hire the best. Collaborate easily. Get quality work.

    Join & Write a Comment

    I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
    Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
    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…

    729 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

    21 Experts available now in Live!

    Get 1:1 Help Now