SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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>'

    Ajax code :

    Code JavaScript:
    <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 Code:
    <?php
    $message
    =$_POST["name"] . "<br>";
    $message=$message $_POST["email"] . "<br>";
    $message=$message $_POST["message"] . "<br>";
    mail("xxx@gmail.com","mail via contact form",$message);
    ?>

  2. #2
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    you forgot to set setRequestHeader (it's request for post call)
    Code JavaScript:
    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 )

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


    Bye.

  3. #3
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by whisher View Post
    Hi,
    you forgot to set setRequestHeader (it's request for post call)
    Code JavaScript:
    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 )

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


    Bye.
    awesome thanks whisher !!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •