Reputation: 1405
I am trying to initiate an ajax post call to a php script where it will update some values in my database.
The problem I have is, the POST values are not seen in my php script, or at least I have no response.
Here is my javascript:
<script type="text/javascript">
$("btnOpsb" ).onclick = pinOpsb;
function pinOpsb(){
var params = "wxdata = test";
var ajaxRequest;
try{ajaxRequest = new XMLHttpRequest();} catch (e){try{ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {try{ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");} catch (e){alert("Error: Browser/Settings conflict");return false;}}}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
alert(ajaxRequest.responseText);
}
}
ajaxRequest.open("POST","wx2strip.php",true);
ajaxRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajaxRequest.send("fname=Henry&lname=Ford");
}
</script>
In my wx2strip.php I have tried echo back something if $_POST had anything in it, but even when I just echo something at the top of the script and exit, there is still no response in the alert I created.
I tried using get, and I do get a response back.
Any help would be appreciated, thanks.
Upvotes: 1
Views: 145
Reputation: 9699
I suspect that Aerik's answer is correct about why you are experiencing problems.
That said, since you are already using jQuery in your javascript, I believe you will simplify things quite a bit if you look into using jQuery's $.post()
. It will handle all of these types of complications for you, so if there are any other small issues like that, you won't run into problems.
Upvotes: 1
Reputation: 2317
Add these two lines to set the right headers:
ajaxRequest.setRequestHeader("Content-length", "fname=Henry&lname=Ford".length);
ajaxRequest.setRequestHeader("Connection", "close");
Content-length is required for POST.
Upvotes: 2