I have a problem with my http.responseText (is always empty). I post my code:
function bCheckName ()
{
// It checks if the browser can allow a http request
if ( window.XMLHttpRequest )
{
xhttp = new XMLHttpRequest();
}
else
{
// for IE6, IE5
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
// It takes the name from the form
var firstName = document.getElementById("firstName").value;
var datastring = "firstName=" + firstName;
var datastring_escaped = encodeURIComponent ( datastring );
// It opens the request to thye server
xhttp.open ( "POST", "../form/formValidation.php", true );
// It sets the header
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// It sends the data to the server
xhttp.send( datastring_escaped );
// It takes the responde from the server
xhttp.onreadystatechange = function()
{
if ( xhttp.readyState == 4 && xhttp.status == 200 )
{
var string = xhttp.responseText.substr ( 0, 2 );
var response = xhttp.responseText.substr ( 5 );
if ( string == "OK")
{
document.getElementById("nameResponse").innerHTML = '<img src = "../img/pages/contact/true.png" alt = "correct answer" >';
document.getElementById("response").innerHTML = response;
}
else
{
document.getElementById("nameResponse").innerHTML = '<img src = "../img/pages/contact/error.png" alt = "wrong answer">';
document.getElementById("response").innerHTML = response;
}
}
}
return false;}
If I replace "xhttp.send( datastring_escaped );" with "xhttp.send( datastring );", everything will work as expected. How can I fix the problem. I post also the php code:
if ( isset( $_POST['firstName'] ))
{
echo( "OK - ".urldecode ( $_POST['firstName']) );
}
How can I solve the problem? Thanks in advance!!!
Francesco