2

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..

Anthony
37.3k26 gold badges105 silver badges168 bronze badges
asked Feb 11, 2010 at 10:07

5 Answers 5

2

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");
answered Feb 11, 2010 at 10:11
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Feb 11, 2010 at 10:10

Comments

1

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.

answered Feb 11, 2010 at 10:16

Comments

0

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.

answered Feb 11, 2010 at 10:12

Comments

0

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)
answered Feb 11, 2010 at 10:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.