Canttouchit
Canttouchit

Reputation: 3159

how to get string from xmlhttp.send(str) via POST in PHP

My code looks like that: Client side JavaScript:

xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST",url + page,true);
xmlhttp.send(str);

I'm missing the code to in PHP side to extract this string, which I assume is in the http post body.

Is it possible to send string array or is this method is restricted for xmls and strings?

Upvotes: 0

Views: 737

Answers (2)

CoR
CoR

Reputation: 3914

As Canttouchit said, these headers must be send for any POST request:

xhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); // for POST escaped 'form' data
xhttp.setRequestHeader("Content-length", post_str.length);
xhttp.setRequestHeader("Connection", "close");

Upvotes: 0

Quentin
Quentin

Reputation: 943569

You can send any data you like.

Usually, you would encode the data as application/x-www-form-urlencoded:

var data = "foo=" + encodeURIComponent(data) + "&bar=" + encodeURIComponent(more_data);
xmlhttp.send(data);

And then access it via $_POST['foo'] and $_POST['bar'].

If you want to access the raw data, then you can access it via file_get_contents('php://input');

Use setRequestHeader to specify the content type of the data you are sending.

Upvotes: 2

Related Questions