SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Always learning viveknarula's Avatar
    Join Date
    Mar 2006
    Location
    INDIA
    Posts
    418
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Problem with ajax and sending mail in PHP

    hello friends,
    Look at the Code below

    This is the mail.php
    Code:
    <?PHP
    $u="name";
    $m="yelolaok@aadcom";
    $body="<html><body><BR>Name:".$u."<BR>Email Address:".$m."<BR></body></html>";
    /* To send HTML mail, you can set the Content-type header. */
    $headers1 = "MIME-Version: 1.0\r\n";
    $headers1 .= "Content-type: text/html; charset=iso-8859-1\r\n";
    
    /* additional headers */
    $headers1 .= "From: ur@yahoo.co.in\r\n";
    $headers1 .= "Reply-To: ur@yahoo.co.in\r\n";
    
    $hello=mail("myid@yahoo.com","ajaxian test",$body,$headers1);
    if($hello)
       {
          echo "gone";
           }
    	   
    	    else
    		    {
    			 echo "not gone";
    			    }
    ?>
    and below is the html file
    Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script language="javascript">
    var xmlhttp;
    function conn()
    {
       if(window.ActiveXObject)
       {    
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	   }
    		  else if(window.XMLHttpRequest)
    		     {
    			    xmlhttp=new XMLHttpRequest();
    			     }	 	   
    				 
       }
    	
    function mkrequest()
     {
        conn();
    	
    	xmlhttp.onreadystatechange=getting;
    	xmlhttp.open("POST","mail.php",true);
    	
    	xmlhttp.send(null);
       }	 
    
    function getting()	
     {  
       
       if(xmlhttp.readystate==4)
         {
    	 //alert(xmlhttp.readyState);
    	 //document.write("i hv sent the mail");
    	   if(xmlhttp.status==200)
    	      {
    		    var get_result=document.getElementById("divi");
    			    get_result2=xmlhttp.responseText;
    				
    				
    				if(get_result2) 
    			       {
    				    get_result.innerHTML="Thnaks for yOUR REPLY WE WILL CONTACT U SOON";
    					
    				      }
    		     			else
    						   {
    						    get_result.innerHTML="NOT ABLE TO GET THE MAIL TRY AGAIN" ;
    						          }
    				}
            }
     }
    	   
    </script>
    </head>
    <body>
    <div id="divi"></div><br />
    <input type="button"  value="send mail" onclick="return mkrequest()" />
    </body>
    </html>

    NOW my problem is that after sending the mail i am not able to display the msg. like "i hv sent the mail"

    when i put the simple msg. or an alert immediately after getting() function starts then it displays.

    But the same msg. is not displying inside if(xmlhttp.readystate==4)
    i donot know why?


    when try to alert the xmlhttp.readystate just after getting function starts it displying all states like 1,2, 3 and 4.

    i am able to sending the mail anywhy. but not able to displying any sort of msg. after if(xmlhttp.readystate==4)

    How this can be accomplish?

    Thanks

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    NOW my problem is that after sending the mail i am not able to display the msg. like "i hv sent the mail"
    There is no message in your code saying "i hv sent mail". If you are trying to display one of these messages:
    Code:
    if(get_result2) 
    {
    	get_result.innerHTML="Thnaks for yOUR REPLY WE WILL CONTACT U SOON";
    
    }
    else
    {
    	get_result.innerHTML="NOT ABLE TO GET THE MAIL TRY AGAIN" ;
    }
    Then your code works fine in IE6, but FF1.0 is not so charitable because of a mistake in your js:
    Code:
    if(xmlhttp.readystate==4)
    There is no "readystate" property--it's "readyState". So FF1.0 skips over that whole if statement block, which is the heart of the code.

    You also have another problem with your js. No matter what you echo here in your php code:
    Code:
    if($hello)
    {
       echo "gone";
    }
    else
    {
       echo "not gone";
    }
    the condition in your js code will always be true:
    Code:
    get_result2=xmlhttp.responseText;
    
    if(get_result2) 
    {
    	get_result.innerHTML="Thnaks for yOUR REPLY WE WILL CONTACT U SOON";
    
    }
    else
    {
    	get_result.innerHTML="NOT ABLE TO GET THE MAIL TRY AGAIN" ;
    }
    Finally, since you are not sending any data to the server along with your XMLHttpRequest, you can use GET instead:
    Code:
    xmlhttp.open("POST","mail.php",true);


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
  •