Ajax basic form submisstion problem!

Discussion in 'JavaScript' started by afridy, Jan 27, 2011.

  1. #1
    Hai foks,

    i could not figure out whats wrong with my ajax code.
    the mail is not sent.

    <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);
    		 alert("Success");
    		 
    	  }
    	}
      
         
        var name=document.getElimentById("name").value;
        var email=document.getElimentById("email").value;
        var message=document.getElimentById("message").value;
    
        xmlhttp.open("POST","sendmail.php",true);
        xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
        xmlhttp.send("name=name&email=email&message=message");
    
    }
    </script>
    PHP:
    but without this part it works :confused:

        var name=document.getElimentById("name").value;
        var email=document.getElimentById("email").value;
        var message=document.getElimentById("message").value;
    Code (markup):

     
    afridy, Jan 27, 2011 IP
  2. buddyborg

    buddyborg Guest

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you are spelling element wrong..getElimentById("email") != .getElementById("email")
     
    buddyborg, Jan 27, 2011 IP
  3. afridy

    afridy Well-Known Member

    Messages:
    810
    Likes Received:
    116
    Best Answers:
    0
    Trophy Points:
    135
    #3
    Hai buddy,
    yes ofcase !!!
    Thank you so much.
    then i have another small problem.

       var name=document.getElementById("name").value;
        var email=document.getElementById("email").value;
        var message=document.getElementById("message").value;
      
        xmlhttp.open("POST","sendmail.php",true);
        xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
        xmlhttp.send("name=name&email=email&message=message");
    
    Code (markup):
    is this line syntax above correct?
    xmlhttp.send("name=name&email=email&message=message");
    Code (markup):
    cos i dont get variable values :D
     
    afridy, Jan 27, 2011 IP
  4. buddyborg

    buddyborg Guest

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    it is incorrect. you need to break up that string.
    xmlhttp.send("name=name&email=email&message=message");

    should be

    xmlhttp.send("name="+name+"&email="+email+"&message="+message);

    or something like that
     
    buddyborg, Jan 27, 2011 IP
  5. afridy

    afridy Well-Known Member

    Messages:
    810
    Likes Received:
    116
    Best Answers:
    0
    Trophy Points:
    135
    #5
    Thanks buddyborg..
    ill try yoru suggesion!!
     
    afridy, Jan 27, 2011 IP