David Garcia
David Garcia

Reputation: 2696

PHP jQuery and Ajax sending / receiving variables & data

So within my jQuery I am making an AJAX call to set a cookie and use the same data to echo the result:

jQuery

jQuery.ajax({
        url: 'script.php',
        data: {status: 'enabled'}
        });

PHP

if(!empty($_GET['status'])) {
   $value = $_GET['status'];
   echo $value;
   setcookie("status", $value, time()+3600, "/");
}

The confusing part is that the cookie is being set, however the value is not being echoed, i also tried to print it but doesnt work either.

Ultimately what I wish to do is use the data passed through the ajax call and assign it to a php variable to be used for some conditionals.

Am I missing something? I am learning how to program.

update request in network

Request URL:http://localhost/wp-content/plugins/lu-ban/inc/lu_ban.php?status=enabled
Request Method:GET
Status Code:200 OK

Upvotes: 0

Views: 1006

Answers (2)

DevlshOne
DevlshOne

Reputation: 8457

Then you'll have to use your Web Developer's tools in your browser to trace the HTTP requests and examine the headers (for the data sent TO the script) and the Responses (for the data being returned).

From the jQuery $.ajax documentation

The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object. For example, it contains responseText and responseXML properties, as well as a getResponseHeader() method.

Upvotes: 1

Akshay Khandelwal
Akshay Khandelwal

Reputation: 1570

You need to see it in script then you need a success callback Here

jQuery.ajax({
   url: 'script.php',
   data: {status: 'enabled'},
   success: function(returnedData){
                  alert(returnedData)
            }  
});

Upvotes: 1

Related Questions