views:

150

answers:

5

What does post mean in the following?

ajaxRequest = new XMLHttpRequest();
ajaxRequest.open("POST", "url" + queryString, true);

because i'm not able to access variables using $_POST['var'] from url but with $_REQUEST['var'] I can access value..

A: 

Your are not able to access the parameters via $_POST because you append them to the URL (i.e. they can be accessed via $_GET) and don't send them as POST data.

If you want to send the parameters via POST, have a look at the send() method.

Felix Kling
+1  A: 

When you read from $_POST, you should pass your arguments in the HTTP body instead of using the querystring.

You would need to send your arguments as in the following example:

ajaxRequest = new XMLHttpRequest();
ajaxRequest.open("POST", "your_service.php", true);
ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
ajaxRequest.send("var=100&another_var=200");
Daniel Vassallo
A: 

The post does mean the values are posted, but you should add them as post variables, while now you are only adding them to the url so you can only get them with $_REQUEST and $_GET.

Matijs
A: 

Post data is usually passed in via the post data.

IIRC, you can pass it as an object via the send method.

ajaxRequest.send(requestString)
Ikke
A: 

POST is something included in an HTTP request (such as an XMLHTTPRequest).

In your case, you are adding the query string to the URL, which means that it is being passed as a GET variable. Even if it is a post request, PHP can still access any GET variables added on as a query string.

Based on your code, I don't think you are telling the request what info should be included in the POST section of the request, which would explain why you are not seeing anything with $_POST['var'].

But since $_REQUEST['var'] looks for request variables in GET and POST and any cookies passed in the request, you see the variable as it was passed via the query string.

Try echoing $_GET['var'] and you'll see that this is where the variable is getting the data from.

If you want to use POST the right way, you need to not point the request to a URL that has a query string and instead define that query string as the post data.

Anthony