Jump to content

AJAX Post and PHP


bobs

Recommended Posts

[u]Ajax using POST to call PHP service.[/u]

I am trying to use Ajax with the POST method to call a PHP service and am failing!

I have written a test service in PHP which receives 3 parameters and simply returns a string including the value of each parameter plus the value of $HTTP_RAW_POST_DATA. The parameter values supplied are obtained from the $_POST array, e.g. $par1 = $_POST['par1'].

I am running php 5.0.5 on apache 1.3.23.

If I call the test service from an html form with method set to POST the service returns what I expect except that the value of $HTTP_RAW_POST_DATA is null. Example result is

par3 = 12345, par2 = formorder, par1 = message, raw post =.

If I call the service using AJAX in javascript, the service response shows that the parameters have either not been transmitted or have not been picked up by PHP. Example result is par3 = , par2 = , par1 = , raw post = .

The Javascript calling the service is

function ajaxFunction(){
var ajaxRequest;  // The variable that makes Ajax possible!
      var poststring;  // message sent as post

try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.holderform.holderspot.value = ajaxRequest.responseText;
}
}
ajaxRequest.open("POST", "ajaxposttest.php", true);
        poststring = "par3=56789&par2=ordertext&par1=message";
ajaxRequest.send(poststring);
}

Obviously something rather wierd is going on here which I just do not understand. Why can I use a POST method form but not a POST method Ajax request. Do I need to set content-type and or content-length? To what? Must be something simple I am missing!!

Bob Snowdon.
Link to comment
https://forums.phpfreaks.com/topic/30132-ajax-post-and-php/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.