My first Ajax form project successfully failed :-(

Hai folks,

i am not receiving the data posted in the ajax form. but i get the email. Body text only show β€˜<br><br><br>’ :frowning:

Ajax code :

<script language="javascript">
function submitform(){

var xmlhttp;

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    //alert(xmlhttp.responseText);
    }
  }
  
xmlhttp.open("POST","sendmail.php",true);
xmlhttp.send("name=Nat&email=dunn@gmail.com&message=test");
}
</script>

PHP code send (sendmail.php) :

<?php
$message=$_POST["name"] . "<br>";
$message=$message . $_POST["email"] . "<br>";
$message=$message . $_POST["message"] . "<br>";
mail("xxx@gmail.com","mail via contact form",$message);
?>

Hi,
you forgot to set setRequestHeader (it’s request for post call)


xmlhttp.open("POST","sendmail.php",true);
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlhttp.send("name=Nat&email=dunn@gmail.com&message=test");

take a look for a better xhr function ( but there are just a lot out there :slight_smile: )

http://www.quirksmode.org/js/xmlhttp.html

Bye.

:smiley: awesome thanks whisher !!!