Thisguydontknow
Thisguydontknow

Reputation: 45

Passing variables with URL not working quite right using PHP and ajax

I'm using AJAX to update a page every 5000 milliseconds. It works great but I have ran into one issue. When I try and get data that is in the URL using $_GET or $_POST it does not work. It instead returns a value of a 1. Here is some example code.

In main.php I have this:

$(document).ready(function worker() {
    $.ajax({
        url: 'Request.php', 
        type: 'POST',
        success: function(data) {
            $('#Live_data').html(data);
        },
        complete: function() {      
            setTimeout(worker, 5000);
        }
    });
})();

and when this is called it fires off the request.php. In request.php I have some code to grab what was added in the URL by a previous page but it dose not work. It goes something like this:

$value = $_get['test'];

This is supposed to return the value in the URL parameter test but it does not work.

Thanks!

Upvotes: 2

Views: 624

Answers (3)

Raj Verma
Raj Verma

Reputation: 1

Your not sending any data here. You can send the required data in Url or in Data Field.

url: 'Request.php?test=xyz', 

or

data: data,

Upvotes: 0

Peter
Peter

Reputation: 9113

I've commented it as well but I'll post it as answer:

  1. Change POST to GET in your jQuery AJAX request.
  2. Use request.php instead of Request.php or the other way around.
  3. Use $_GET instead of $_get. This variable is case sensitive.

Upvotes: 1

user4466350
user4466350

Reputation:

You forgot to send data with the ajax query,

In this code, you can add GET data by append a query string to url value, or send POST data by setting data property of the request,

    $.ajax({
        url: 'Request.php?query=string&is=here', 
        type: 'POST',
        data: {to: 'post', goes: 'here'},
        success: function(data) {
            $('#Live_data').html(data);
        },
        complete: function() {      
            setTimeout(worker, 5000);
        }
    });

see also https://api.jquery.com/jquery.post/#jQuery-post-settings

Upvotes: 3

Related Questions