POST or GET in XMLHttpRequest - php

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

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");

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.

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.

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.

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)

Related

$_POST not populated

I'm having some issues with the PHP REST API of my app.
I'm submitting my parameters in the correct URL format: baseUrl?param1=val1&param2=val2
However, the parameters are being filled into the $_GET array instead. I've looked into the $_SERVER array to see if there is something wrong there, but I can't see anything obvious.
$_SERVER['CONTENT_TYPE']="application/x-www-form-urlencoded"
$_SERVER['REQUEST_METHOD']="POST"
I've also checked my php.ini for post_max_size misconfig:
post_max_size=32M
I'm not able to figure what else could cause the parameters to be placed into $_GET
If it helps, I'm using XAMPP on Windows.
Also, using "Advanced REST client" for Chrome, I've noticed that regardless of the method, POST parameters are placed into $_GET if the parameters are in the URL, but if I move them into the REST client's Payload field, they show up as POST parameters.
You cannot get parameters via $_POST which are submitted in query-string. You can get them via $_GET array.
but if I move them into the REST client's Payload field, they show up as POST parameters., seeing your trouble, you can always get them with $_REQUEST
Just sharing a thought, The place from where you are initiating post request to backend, you have to use post method.
For example if you are using jquery
$.ajax ({ type: 'POST' })
If angular js
$http.post({ 'your code' });

RESTFullYii does not get data for PUT request

I use RESTFullYii v1.15 from here . RESTFullYii does not get data for PUT request
I faced the problem that making PUT method the data I submit are not inside the doRestUpdate($id, $data) function.
In the link above it is defined as public function doRestUpdate($id, $data).
So it is supposed that data are submit are in $data variable.
Vars $_GET, $_POST, $_REQUEST don't have these data too.
I watch headers of request using firebug and see that data are sent in PUT request, but they are not in doRestUpdate function.
If there is way to fix it?

$_POST empty in PHP file but call it with POST variables in the URL

I call my php file in the browser with "...test.php?text=Hello" but the $_POST variable stays empty (also print_r($_POST) returns array() ).
Why? Do I need to activate the post variable or something?
Thanks.
Variables passed in through the URL end up inside $_GET, not $_POST.
$_POST contains variables parsed by reading the HTTP request body when the method is POST. In this case the method is not POST and there is also no request body.
If you pass the variable through the URL you use $_GET.
Also, you will access the variable with:
$_GET['text']
This is a array that is sent through and you need to specify what item in the array you want to use.
...test.php?text=hello passes data via the GET method (accessible via $_GET in the processing script).
$_POST gets populated by forms or cURL access (when the transfer method is defined as "post")

javascript post to .php file one server does not work - manual entry of URL in browser to same .php works

i had some issues with understanding how to get javascript (client) variables transferred so they were acessible from php (serverside) as session : get an iframe's "src" value in PHP?
Now im in a situation where i use firebug to try to debug whats going on, but it just doesnt make sense :
i have this function to update an iframe and i want to pass on the page that that iframe is displaying :
function frameclick(pageurl)
{
$.post("session_write.php?",
{
frameurl : pageurl
}
$("#iFrame1").attr('src', pageurl);
console.log ('<?php echo "logout:".$langpath.$_SESSION['frameurl'];?>');
}
pageurl is ex. "/lang/en/new.htm" - and if i inspect it with firebug i also can see it says that it passes it correctly ( also with conversion of /).
my script serverside that its posted to is like this :
#session_write.php
<?php
session_start();
print_r($_GET['frameurl']);
if (isset($_GET['frameurl']))
{
$_SESSION['frameurl'] = $_GET['frameurl'];
print_r($_SESSION);
}
?>
Posting to that php script on the server will fail via the javascropt - $_SESSION['frameurl'] will be '', but if i ex. do it manually like this :
(http):
//localhost/phpmenu/session_write.php?frameurl=lang%2Fen%2Fnew.htm
then it will be correctly set in the $_SESSION["frameurl"] variable.
I simply cannot understand whats different between doing the javascript post and doing it manually in the browser and why its causing me this problem ?
anyone with an idea ? thanks
You are using .post, which executes a POST request, but when you type in the URL in the address bar, that is a GET request.
$_GET retrieves any params passed through GET, while $_POST retrieves any params passed through POST. So if you use .post with Javascript but try to retrieve with $_GET in PHP, it wouldn't work.
When you POST variables to a PHP file, $_GET is not set. Use $_POST['frameurl'] instead. Also, it looks like you're missing a close paren in frameclick to end the post call.
You are passing data via a
POST request and retrieving for all the GET requests. Use $_POST instead. You may also be interested in $_REQUEST

Url PUSH Issue in $_POST Method

Currently i have been working on a project which connects to a smsgateway. Once the user sends sms to gateway it redirects the requests to our server. Problem is they are received in $_GET method. But the sms provider says they are passing it in $_POST method. Url received at our end looks like the following.
http://www.example.com/smstest?msg=sample&id=55788
Is it possible to receive parameters in url when you use the $_POST method
An HTTP request can use the HTTP method POST and still use a URL that contains query parameters. POST is just a "verb" used in the HTTP header, the same as GET (and PUT and DELETE). A URL can always contain query parameters and it can also always contain a request body (though GET requests shouldn't). The PHP variable $_GET simply represents the parsed URL query parameters, the variable $_POST simply represents the parsed request body. They do not actually have anything to do with the HTTP verb and are therefore somewhat misnamed.
Yes, it is. The first line of every HTTP request contains the method (or verb) and the URI for which the request is made. No special restrictions are placed on the URI based on the choice of method, so POST requests may be made for a URI that includes a query string.
From PHP you can access the parameters in the query string normally through $_GET and $_REQUEST. Parameters passed as part of a submitted form are accessible as always through $_POST and $_REQUEST.
You can only have one verb (POST, GET, PUT, ...) when doing an HTTP Request. However, you can do
<form name="y" method="post" action"y.php?foo=bar">
and then PHP will populate $_GET['foo'] as well, although the Request was POST'ed.
For comment
By using
$_SERVER['REQUEST_METHOD']

Categories