I am using an html sign up form for a website I’m building. The form accepts first name, last name, email address, birthdate (month,day,year), and sex. It calls a php file to process the results and email them to me like this:
<form method="post" action="sendMail.php" >
the php file looks like:
<?php
$firstName = $_REQUEST['FirstName'] ;
$lastName = $_REQUEST['LastName'] ;
$email = $_REQUEST['EmailAddress'] ;
$birthdate = $_REQUEST['month'] ;
$sex = $_REQUEST['sex'] ;
$message .= "First Name: \t";
$message = $firstName;
$message .= "\n";
$message .= "Last Name: \t";
$message .= $lastName;
$message .= "\n";
$message .= "Email: \t";
$message .= $email;
$message .= "\n";
$message .= "Birthdate: \t";
$message .= $birthdate;
$message .= "\n";
$message .= "Sex: \t";
$message .= $sex;
mail( "ty@ollielifestyle.com", "Sign-Up Form Results", $message, "From: $email" );
/*?> header( "Location: http://www.example.com/thankyou.html" );
<?php */
?>
The script works fine to process and email the results, but I do not know how to make the ‘php mail() function’ return to the exact same calling page. I know that typically people like to show a thank you page but I do not want to lead the visitor to just a thank you page. Is there a way to alter the ‘php mail() function’ to redirect to the same exact page that called it, or to call it without leaving the page in the first place? Thanks for any hints in advance!
Update I have included the php into the same page with the sign up form. It isn’t returning any errors but I do not seem to be getting any emails delivered to the gmail account I set up for it. I did not know if it would work better to place the form in the html file as I normally would (outside of the php function) or inside an else statement so I tried it both ways with neither giving an email. Was there a logical error in the code below ?
Version 1:
$message .= "First Name: \t";
$message = $firstName;
$message .= "\n";
$message .= "Last Name: \t";
$message .= $lastName;
$message .= "\n";
$message .= "Email: \t";
$message .= $email;
$message .= "\n";
$message .= "Birthdate: \t";
$message .= $birthdate;
$message .= "\n";
$message .= "Sex: \t";
$message .= $sex;
mail( "ymaxwell86@gmail.com", "Sign-Up Form Results", $message, "From: $email" );
echo "Thank you for using our mail form";
}
?>
First Name:*
Last Name:*
Email Address:*
Birthday:
Month
January
February
March
April
May
June
July
August
September
October
November
December
Day
Year
function changeDate(i){
var e = document.getElementById(‘day’);
while(e.length>0)
e.remove(e.length-1);
var j=-1;
if(i==”na”)
k=0;
else if(i==2)
k=28;
else if(i==4||i==6||i==9||i==11)
k=30;
else
k=31;
while(j++1909){
var s = document.createElement(‘option’);
var e = document.getElementById(‘year’);
s.text=y;
s.value=y;
try{
e.add(s,null);}
catch(ex){
e.add(s);}}
Gender:
Male
Female
*required fields
Version 2:
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$firstName = $_REQUEST['FirstName'] ;
$lastName = $_REQUEST['LastName'] ;
$email = $_REQUEST['EmailAddress'] ;
$birthdate = $_REQUEST['month'] ;
$sex = $_REQUEST['sex'] ;
$message .= "First Name: \t";
$message = $firstName;
$message .= "\n";
$message .= "Last Name: \t";
$message .= $lastName;
$message .= "\n";
$message .= "Email: \t";
$message .= $email;
$message .= "\n";
$message .= "Birthdate: \t";
$message .= $birthdate;
$message .= "\n";
$message .= "Sex: \t";
$message .= $sex;
mail( "ymaxwell86@gmail.com", "Sign-Up Form Results", $message, "From: $email" );
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<form action='' method='post'>
<p>First Name:*
<input name='FirstName' type='text' id='FirstName' size='25' /> <br />
Last Name:*
<input name='LastName' type='text' id='LastName' size='25'/><br />
<br />
Email Address:*
<input name='EmailAddress' type='text' id='EmailAddress' size='25' onblur='valid_email=validateEmail(email_field,mandatory,messages);' />
<br />
<br />
Birthday:
<select name='month' onChange='changeDate(this.options[selectedIndex].value);'>
<option value='na'>Month</option>
<option value='1'>January</option>
<option value='2'>February</option>
<option value='3'>March</option>
<option value='4'>April</option>
<option value='5'>May</option>
<option value='6'>June</option>
<option value='7'>July</option>
<option value='8'>August</option>
<option value='9'>September</option>
<option value='10'>October</option>
<option value='11'>November</option>
<option value='12'>December</option>
</select>
<select name='day' id='day'>
<option value='na'>Day</option>
</select>
<select name='year' id='year'>
<option value='na'>Year</option>
</select>
<script language='JavaScript' type='text/javascript'>
function changeDate(i){
var e = document.getElementById('day');
while(e.length>0)
e.remove(e.length-1);
var j=-1;
if(i=='na')
k=0;
else if(i==2)
k=28;
else if(i==4||i==6||i==9||i==11)
k=30;
else
k=31;
while(j++<k){
var s=document.createElement('option');
var e=document.getElementById('day');
if(j==0){
s.text='Day';
s.value='na';
try{
e.add(s,null);}
catch(ex){
e.add(s);}}
else{
s.text=j;
s.value=j;
try{
e.add(s,null);}
catch(ex){
e.add(s);}}}}
y = 2010;
while (y-->1909){
var s = document.createElement('option');
var e = document.getElementById('year');
s.text=y;
s.value=y;
try{
e.add(s,null);}
catch(ex){
e.add(s);}}
</script>
<br />
</p>
<p>Gender:
<input type='radio' name='sex' value='male' /> Male
<input type='radio' name='sex' value='female' /> Female
<br />
<div id='mailListNote'>
*required fields
</div>
<br />
<br />
<br />
<input type='submit' name='submit' value='Submit' onclick='valid_email =validateEmail(email_field,mandatory,messages); '/>
<INPUT TYPE=RESET onClick='return confirm('Are you sure you want to reset the form?')'>
</p>
</form>";
}
?>
You have basically two options:
header("location:…");to return to the original page.It is definitely a good idea to provide some feedback to the user after submitting your form. So, the thank you page might just be the most user-friendly solution. And even the technically easiest solution for you.
Hope this helps!