Solved

How to do a AJAX query in php

Posted on 2014-01-29
3
356 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
ID: 39819419
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
ID: 39819490
$.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
ID: 39821983
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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…

920 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

13 Experts available now in Live!

Get 1:1 Help Now