Solved

How to do a AJAX query in php

Posted on 2014-01-29
3
372 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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

624 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