Solved

How to do a AJAX query in php

Posted on 2014-01-29
3
369 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
[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 Comments
 
LVL 35

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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)

751 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