Hi,

I am using mail function of php to send email. I have a html page in which there is a form after submission of the form it redirected to mail.php and send email then i want to redirect back to html form with some message say "Mail sent successfully".

Please let me know how to do this.

Thanks

your form page, say myform.html.

<div><?php echo $_GET['message']?> </div>
<form action='mail.php' >
.
.
.

</form>

your mail.php

<?php
$mailsent=mail('som@g.com','hi','how are you');

if($mailsent)
   $msg="Mail sent succefully";
else
   $msg="Error occured while sending mail";

header("location:myform.html?message={$msg}");
exit;

?>

Hi,
Thanks for your reply, following are the code which i have used to send email ..
mail.php

<html>
<body>

<?php

  //send email

  $from = 'enquiry@webzymes.com';
  $name = $_REQUEST['txtName'] ;
  $subject = $_REQUEST['txtSubject'] ;
  $company = $_REQUEST['txtCompany'] ;
  $number = $_REQUEST['txtNumber'] ;
   $email = $_REQUEST['txtEmail'] ;
  $msg = $_REQUEST['txtarMessage'] ;
  $message= " Name : " .$name." \n Company : ".$company." \n Contact Number :".$number." \n Email : ".$email." \n Message : ".$msg;
 $mailsent= mail("nikita.chandra@webzymes.com", "$subject",
  $message, "From:" . $from);
if($mailsent)
   $msg1="Mail sent succefully";
else
   $msg1="Error occured while sending mail";
 
header("location:contactUs.html?message={$msg1}");
exit;
    
  

?>
</body>
</html>

contactUs.html

<!-- 
    Document   : contactUs
    Created on : Jul 15, 2011, 10:58:48 AM
    Author     : Nikita
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Welcome to WebZyems Technologies</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <form name="frmContactUs" method="post" action="mail.php">
	<div id="frame">
             
		<!--header start here -->
 	  <div id="headcontainer">
                           <!-- <font color="red" size="5"><img src="images/globe.jpg" height="150" width="150"/></font>
        	
                   <img src="images/logo.png" height="80" width="400"/>
                   -->
          
                   </div>
<!--header end here -->
	
		<!--body start here -->
			<div id="maincontainer">
				<!--left panel start here -->
					<div id="leftnavcontainer">
						<ul>
							<li><a href="index.html" class="grey" title="About Company">Home</a></li>
							<li><a href="aboutUs.html" class="green" title="About Us">About Us</a></li>
							<li><a href="services.html" class="red" title="Services">Services</a></li>
							<li><a href="contactUs.html" class="orange" title="contact Us">Contact Us</a></li>
							<li><a href="support.html" class="yellow" title="Support">Support</a></li>
						</ul>
                                                <img src="images/logo3.jpg" height="180" width="200"/>
                                                 
						<br class="spacer" />
					</div>
				<!--left panel end here -->
			
				<!--right panel end here -->
					<div id="body">
                                             
                                             <!--welcome part start here -->
						<div id="welcomecontacienr">
                                                     <table border="1" width="500" align="center">
                                                         <tr>
                                                             <td>
                                                                 <table align="center">
                                                                     <tr>
                                                                         <td>
                                                                             Name :
                                                                         </td>
                                                                         <td>
                                                                             <input type="text" name="txtName"/>
                                                                         </td>
                                                                     </tr>
                                                                     <tr>
                                                                         <td>
                                                                             Subject :
                                                                         </td>
                                                                         <td>
                                                                             <input type="text" name="txtSubject"/>
                                                                         </td>
                                                                     </tr>
                                                                     <tr>
                                                                         <td>
                                                                             Company :
                                                                         </td>
                                                                         <td>
                                                                             <input type="text" name="txtCompany"/>
                                                                         </td>
                                                                     </tr>
                                                                     <tr>
                                                                         <td>
                                                                             Contact Number :
                                                                         </td>
                                                                         <td>
                                                                             <input type="text" name="txtNumber"/>
                                                                         </td>
                                                                     </tr>
                                                                     <tr>
                                                                         <td>
                                                                             Email :
                                                                         </td>
                                                                         <td>
                                                                             <input type="text" name="txtEmail"/>
                                                                         </td>
                                                                     </tr>
                                                                     <tr>
                                                                         <td>
                                                                             Message :
                                                                         </td>
                                                                         <td>
                                                                             <textarea rows="5" columns="10" name="txtarMessage"></textarea>
                                                                         </td>
                                                                     </tr>
                                                                     
                                                                     <tr>
                                                                         <td align="center" colspan="2">
                                                                             <input type="submit" name="btnSubmit" value="SUBMIT">
                                                                             <input type="reset" name="reset" value="RESET">  
              
                                                                         </td>
                                                                     </tr>
                                                                 </table>
                                                             </td>
                                                         </tr>
                                                     </table>
                                                     <br/><br/>
                                                 
					<!--footer start here -->
		<!--<div id="footercontainer">
		Copyright © 2011. All rights reserved.<br />
            by WebZymes Technologies
		</div>
	<!--footer end here -->
        
			</div>
		<!--body end here -->			
	</div>					
     </div>
 </form>
</body>
</html>

and when i submit form following error occurs :

Warning: Cannot modify header information - headers already sent by (output started at /home/webzymes/public_html/webzymes.com/mail.php:4) in /home/webzymes/public_html/webzymes.com/mail.php on line 23

Please check this for me as i am learning php and can't understand such errors.

Hi,
Thanks for your reply, following are the code which i have used to send email ..
mail.php

<html>
<body>

<?php

  //send email

  $from = 'enquiry@webzymes.com';
  $name = $_REQUEST['txtName'] ;
  $subject = $_REQUEST['txtSubject'] ;
  $company = $_REQUEST['txtCompany'] ;
  $number = $_REQUEST['txtNumber'] ;
   $email = $_REQUEST['txtEmail'] ;
  $msg = $_REQUEST['txtarMessage'] ;
  $message= " Name : " .$name." \n Company : ".$company." \n Contact Number :".$number." \n Email : ".$email." \n Message : ".$msg;
 $mailsent= mail("nikita.chandra@webzymes.com", "$subject",
  $message, "From:" . $from);
if($mailsent)
   $msg1="Mail sent succefully";
else
   $msg1="Error occured while sending mail";
 
header("location:contactUs.html?message={$msg1}");
exit;
    
  

?>
</body>
</html>

contactUs.html

<!-- 
    Document   : contactUs
    Created on : Jul 15, 2011, 10:58:48 AM
    Author     : Nikita
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Welcome to WebZyems Technologies</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <form name="frmContactUs" method="post" action="mail.php">
	<div id="frame">
             
		<!--header start here -->
 	  <div id="headcontainer">
                           <!-- <font color="red" size="5"><img src="images/globe.jpg" height="150" width="150"/></font>
        	
                   <img src="images/logo.png" height="80" width="400"/>
                   -->
          
                   </div>
<!--header end here -->
	
		<!--body start here -->
			<div id="maincontainer">
				<!--left panel start here -->
					<div id="leftnavcontainer">
						<ul>
							<li><a href="index.html" class="grey" title="About Company">Home</a></li>
							<li><a href="aboutUs.html" class="green" title="About Us">About Us</a></li>
							<li><a href="services.html" class="red" title="Services">Services</a></li>
							<li><a href="contactUs.html" class="orange" title="contact Us">Contact Us</a></li>
							<li><a href="support.html" class="yellow" title="Support">Support</a></li>
						</ul>
                                                <img src="images/logo3.jpg" height="180" width="200"/>
                                                 
						<br class="spacer" />
					</div>
				<!--left panel end here -->
			
				<!--right panel end here -->
					<div id="body">
                                             
                                             <!--welcome part start here -->
						<div id="welcomecontacienr">
                                                     <table border="1" width="500" align="center">
                                                         <tr>
                                                             <td>
                                                                 <table align="center">
                                                                     <tr>
                                                                         <td>
                                                                             Name :
                                                                         </td>
                                                                         <td>
                                                                             <input type="text" name="txtName"/>
                                                                         </td>
                                                                     </tr>
                                                                     <tr>
                                                                         <td>
                                                                             Subject :
                                                                         </td>
                                                                         <td>
                                                                             <input type="text" name="txtSubject"/>
                                                                         </td>
                                                                     </tr>
                                                                     <tr>
                                                                         <td>
                                                                             Company :
                                                                         </td>
                                                                         <td>
                                                                             <input type="text" name="txtCompany"/>
                                                                         </td>
                                                                     </tr>
                                                                     <tr>
                                                                         <td>
                                                                             Contact Number :
                                                                         </td>
                                                                         <td>
                                                                             <input type="text" name="txtNumber"/>
                                                                         </td>
                                                                     </tr>
                                                                     <tr>
                                                                         <td>
                                                                             Email :
                                                                         </td>
                                                                         <td>
                                                                             <input type="text" name="txtEmail"/>
                                                                         </td>
                                                                     </tr>
                                                                     <tr>
                                                                         <td>
                                                                             Message :
                                                                         </td>
                                                                         <td>
                                                                             <textarea rows="5" columns="10" name="txtarMessage"></textarea>
                                                                         </td>
                                                                     </tr>
                                                                     
                                                                     <tr>
                                                                         <td align="center" colspan="2">
                                                                             <input type="submit" name="btnSubmit" value="SUBMIT">
                                                                             <input type="reset" name="reset" value="RESET">  
              
                                                                         </td>
                                                                     </tr>
                                                                 </table>
                                                             </td>
                                                         </tr>
                                                     </table>
                                                     <br/><br/>
                                                 
					<!--footer start here -->
		<!--<div id="footercontainer">
		Copyright © 2011. All rights reserved.<br />
            by WebZymes Technologies
		</div>
	<!--footer end here -->
        
			</div>
		<!--body end here -->			
	</div>					
     </div>
 </form>
</body>
</html>

and when i submit form following error occurs :

Warning: Cannot modify header information - headers already sent by (output started at /home/webzymes/public_html/webzymes.com/mail.php:4) in /home/webzymes/public_html/webzymes.com/mail.php on line 23

Please check this for me as i am learning php and can't understand such errors.

Use Following.. instead header() function..

$url=www.two.com
echo "<meta http-equiv=\"refresh\" content=\"1;URL=$url\" />

I have removed <html><body>, spaces, new lines from your mail.php, to avoid header erros.

<?php

  //send email

  $from = 'enquiry@webzymes.com';
  $name = $_REQUEST['txtName'] ;
  $subject = $_REQUEST['txtSubject'] ;
  $company = $_REQUEST['txtCompany'] ;
  $number = $_REQUEST['txtNumber'] ;
   $email = $_REQUEST['txtEmail'] ;
  $msg = $_REQUEST['txtarMessage'] ;
  $message= " Name : " .$name." \n Company : ".$company." \n Contact Number :".$number." \n Email : ".$email." \n Message : ".$msg;
 $mailsent= mail("nikita.chandra@webzymes.com", "$subject",
  $message, "From:" . $from);
if($mailsent)
   $msg1="Mail sent succefully";
else
   $msg1="Error occured while sending mail";
 
header("location:contactUs.html?message={$msg1}");
exit;
?>

attach mail.php file here using advance editor

Hello,

You cannot use the header() function after you have already displayed some information to the page.

You are displaying tons of info, and only after that are you using the header function().

If you want to use the header() function, you must do it before you display any output, like right at the top of the page, before you any html or any echo.

There are two sollution I can think of. A quick way is to use JavaScript to redirect you to the main page.

Replace this line in the login.php

header("location:contactUs.html?message={$msg1}"); // Goes to main page.

with this one

echo "<script>window.location = 'contactUs.html?message={<?php echo $msg1;?>}'</script>"; // Goes to main page.

The second way to solve this is to use something called "output buffering".
I have never really played around with this, but from what I've heard, this is what it does:
It automatically hold back any information you are trying to display until the last second, and then only display it.
While the output is being held back, you have a change to use the header() function.

Right at the top of you index.php file u use this funtction:

ob_start();

and right at the end of your index.php you use this function:

ob_end_flush();

Hi,

Thanks for reply.

When i used javascript it works. But i need to display the success message on my page, it is appended to addressbar.

How to display message on form.

Thanks

change your main form file like this

<div><?php echo $_GET['message']?> </div>
<form action='mail.php' >
.
.
.

</form>

Hi,

The code given above in the post displays message in address bar not on html page.

Thanks

post your latest html page and mail.php page here using advance editor

also you are using echo $_GET but in url you can see variable name is message

so change to $_GET in contact form .php

change your main form file like this

<div><?php echo $_GET['message']?> </div>
<form action='mail.php' >
.
.
.

</form>

<?php echo $_GET?>
How can we use php in html form?

Use php form instead of html form to display successful message or else try out with jquery display messages.

Member Avatar for diafol

You don't need any javascript - in fact I'd avoid js for this or you won't get page redirect for somebody with js switched off. Left in limbo?? Best to do it with header().


You can save a msg via sessions or cookies as well as get (querystring). Session probably cleaner, but you can also set querystring to 1 or 0

Don't put any html into this file - not required:

<?php
  //send email
  $from = 'enquiry@webzymes.com';
  $name = $_POST['txtName'] ;
  $subject = $_POST['txtSubject'] ;
  $company = $_POST['txtCompany'] ;
  $number = $_POST['txtNumber'] ;
   $email = $_POST['txtEmail'] ;
  $msg = $_POST['txtarMessage'] ;
  $message= " Name : " .$name." \n Company : ".$company." \n Contact Number :".$number." \n Email : ".$email." \n Message : ".$msg;
 $mailsent= mail("nikita.chandra@webzymes.com", "$subject",
  $message, "From:" . $from);
if($mailsent){
   $msg1=1;
}else{
   $msg1=0;
} 
//you should use the full url in header
header("location:contactUs.html?message=$msg1");
exit;
?>

In your mail page:

<?php
...
if(isset($_GET['message'])){
  if($_GET['message'] == 1){
    echo "Sent, the message is";
  }else{
    echo "Sent, not the message is";
  }
}
...
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.