Solved

How to do a AJAX query in php

Posted on 2014-01-29
3
351 Views
Last Modified: 2014-01-30
Hey,

I am doing a website http://www.thecacaocafe.com

I do a JQUERY script.

Can some one tell me how to do a simple AJAX query please in JQUERY to get data from my
data.php?

Thanks  David
0
Comment
Question by:DavidInLove
3 Comments
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 100 total points
Comment Utility
Check the bottom of the page for examples:
http://api.jquery.com/jquery.ajax/
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 400 total points
Comment Utility
$.get("data.php, { param1:"value1", param2:123 }, function(data_or_message_from_data_dot_php) {
       alert( data_or_message_from_data_dot_php );
});

Open in new window


on data.php :
$param1 = $_GET["param1"]; // value1
$param2 = $_GET["param2"]; // 123

Open in new window

You can use $.post and use $_POST on the server side

$.get and $.post are shortcut of $.ajax
$.ajax({
    method: "GET", // or POST
    url: "data.php",
    success: function(data_or_message_from_data_dot_php) {
       alert( data_or_message_from_data_dot_php );
    }
})

Open in new window

0
 

Author Closing Comment

by:DavidInLove
Comment Utility
Thank you,

I have tested this on my website and after one hour
looking for corrections I've managed to have a AJAX GET and POST
THANKS.
The problem with the code I find in books internet... I must always correct something
and loose time.

http://www.thecacaocafe.com/sample_code/ajax/Q_ajax_20140130.html

my comment

With the  link  http://api.jquery.com/jquery.ajax/ I found an example
which fails:


$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})
  .done(function( msg ) {
    alert( "Data Saved: " + msg );
  });


(
it seems the correct code is

 $.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" },
  success: function(data, textStatus, jqXHR)
    {
        alert(data);
    }
});

After 1 hour struggling I've managed to correct the sample code

 
)

but thanks to it I've been able to correct
the source code a little
$.get("data.php,  => $.get("data.php",
and in the second method you ommited
 data: { param1:"value1A", param2:123456 },

$.ajax({
    method: "GET", // or POST
    url: "data.php",
    success: function(data_or_message_from_data_dot_php) {
       alert( data_or_message_from_data_dot_php );
    }
})

=>

$.ajax({
    method: "GET", // or POST
    url: "data.php",
       data: { param1:"value1A", param2:123456 },
    success: function(data_or_message_from_data_dot_php) {
       alert( data_or_message_from_data_dot_php );
    }
      })
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
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…

743 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

17 Experts available now in Live!

Get 1:1 Help Now