PDA

View Full Version : Can never get PHP mail form to work.


andy1212
10-25-2010, 07:35 PM
I tried a few years ago to make php mail forms and they never worked for me. I would follow different tutorials and nothing would work. I just recently got back into designing websites and a client of mine wants me to set up an online survey and have customers fill out the survey (which i've designed in Dreamweaver cs4 in html) and then when they click submit the results would go to her website email (info@website.com, just an example). I followed 3 tutorials from tutvid, how to create a fill out form, how to validate it, and how to create php to send it. When I upload the files to the host, I make sure both files are in the same folder and are in the folder where all the other .html pages for the website are located. When I test the form by going to the url that the form is located and filling it out, then pressing submit, it opens out a blank page in a new tab and nothing happens. When I check the email that the survey is supposed to be sent to, it doesn't show up at all. The host I'm using supports PHP so that wouldn't be the problem but I don't know what it would be because I followed tutvid's tutorials and they usually work for me. I need help with this so thanks in advance! If the code is too confusing to read I can send the files through email to whoever is willing to help me.

Here is the code for the html file which contains the contact form:

<!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=utf-8" />
<title>Online Survey</title>
<style type="text/css">
<!--
#form1 table tr td div label {
font-family: Arial, Helvetica, sans-serif;
}
#form1 table tr td div {
font-family: Arial, Helvetica, sans-serif;
}
.a {
font-family: Arial, Helvetica, sans-serif;
font-size: 24px;
font-weight: bold;
text-align: center;
}
.th1 {
font-family: Tahoma, Geneva, sans-serif;
font-size: 12px;
}
.th2 {
color: #F00;
}
body {
background-color: #C7B299;
background-image: url(../images/back.jpg);
background-repeat: repeat-x;
}
-->
</style>
<script type="text/javascript">
<!--
function MM_validateForm() { //v4.0
if (document.getElementById){
var i,p,q,nm,test,num,min,max,errors='',args=MM_valida teForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
} }
//-->
</script>
</head>

<body>
<div align="center">
<table width="600" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="600" height="79" valign="top" class="a">Online Survey</td>
</tr>
</table>
<table width="600" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" width="600" height="50" class="th1">Fields with (<span class="th2">*</span>) are required to be filled out.</td>
</tr>
</table>
<form action="contactformprocess.php" method="post" name="form1" id="form1">
<table width="600" border="2" cellspacing="0" cellpadding="6">
<tr>
<td><div align="right">
<label for="email2">Email Address:</label>
</div></td>
<td><div align="left">
<input name="email" type="text" id="email2" tabindex="1" size="35" maxlength="90" />
<span class="th2">*</span></div></td>
</tr>
<tr>
<td><div align="right">
<label for="name">Full Name:</label>
</div></td>
<td><div align="left">
<input name="name" type="text" id="name" tabindex="2" size="35" maxlength="80" />
<span class="th2">*</span></div></td>
</tr>
<tr>
<td><div align="right">
<label for="phone">Phone Number:</label>
</div></td>
<td><div align="left">
<input name="phone" type="text" id="phone" tabindex="3" size="35" maxlength="12" />
e.g. (xxx-xxx-xxxx)
<span class="th2">*</span></div></td>
</tr>
<tr>
<td><div align="right">
<label for="stylist">Stylist's Name:</label>
</div></td>
<td><div align="left">
<input name="stylist" type="text" id="stylist" tabindex="4" size="35" maxlength="80" />
<span class="th2">*</span></div></td>
</tr>
<tr>
<td><div align="right">Store:</div></td>
<td><div align="left">
<p>
<label>
<input type="radio" name="store" value="haircuts" id="Store_0" tabindex="5" />
Haircuts</label><br />
<label>
<input type="radio" name="store" value="haircuts2" id="Store_1" tabindex="6" />
Haircuts2</label><br />
</p>
</div></td>
</tr>
<tr>
<td><div align="right">Stylist Rating</div></td>
<td><div align="left">
<p>
<label>
<input type="radio" name="stylist_rating" value="excellent" id="StylistRating_0" tabindex="7" />
Excellent</label>
<br />
<label>
<input type="radio" name="stylist_rating" value="average" id="StylistRating_1" tabindex="8" />
Average</label>
<br />
<label>
<input type="radio" name="stylist_rating" value="needs improvement" id="StylistRating_2" tabindex="9" />
Needs Improvement</label>
<br />
</p>
</div></td>
</tr>
<tr>
<td><div align="right">Overall Experience</div></td>
<td><div align="left">
<p>
<label>
<input type="radio" name="overall_experience" value="professional" id="OverallExperience_0" tabindex="10" />
Professional</label>
<br />
<label>
<input type="radio" name="overall_experience" value="unprofessional" id="OverallExperience_1" tabindex="11" />
Unprofessional</label>
<br />
</p>
</div></td>
</tr>
<tr>
<td><div align="right">
<label for="comments">Additional Comments:</label>
</div></td>
<td><div align="left">
<textarea name="comments" id="comments" cols="35" rows="5" tabindex="12"></textarea>
</div></td>
</tr>
<tr>
<td><div align="right">
<label for="reset"></label>
<input type="reset" name="reset" id="reset" value="Reset" tabindex="13" />
</div></td>
<td><div align="left">
<label for="submit"></label>
<input name="submit" type="submit" id="submit" tabindex="14" onclick="MM_validateForm('email2','','RisEmail','name','',' R','phone','','R','stylist','','R');return document.MM_returnValue" value="Submit" />
</div></td>
</tr>
</table>
</form>
</div>
</body>
</html>And here is the code for the PHP form.

<?php

/* Subject and email variables */

$emailSubject = 'Online Survey';
$webMaster = 'info@website.com';

/* Gathering data variables */

$email = $_Post['email'};
$name = $_Post['name'};
$phone = $_Post['phone'};
$stylist = $_Post['stylist'};
$store = $_Post['store'};
$stylist_rating = $_Post['stylist_rating'};
$overall_experience = $_Post['overall_experience'};
$comments = $_Post['comments'};

$body = <<<EOD
<br><hr><br>
Email: $email <br>
Name: $name <br>
Phone: $phone <br>
Stylist: $stylist <br>
Store: $store <br>
Stylist Rating: $stylist_rating <br>
Overall Experience: $overall experience <br>
Comments: $comments <br>
EOD;

$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail{$webMaster, $emailSubject, $body, $headers};

/* Results rendered as HTML */

$theResults = <<<EOD
<html>
<head>
<title>Online Survey</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body {
background-color: #f1f1f1;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
font-style: normal;
line-height: normal;
font-weight: normal;
color: #666666;
text-decoration: none;
}
-->
</style>
</head>

<div>
<div align="left">Thank you for your Feedback! You will now be entered into the draw for a pair of gift certificates.</div>
</div>
</body>
</html>
EOD;
echo "$theResults";

?>

DWcourse
10-25-2010, 09:43 PM
I'm no expert but I think you want to send the email instead of defining a variable:

$success = mail{$webMaster, $emailSubject, $body, $headers};

should probably be just:

mail{$webMaster, $emailSubject, $body, $headers};

andy1212
10-26-2010, 01:31 AM
ok i'll try that. I put success because it was mentioned to do so in the tutorial by tutvid but thanks for the help and I'll post back here if it worked or not.

andy1212
10-26-2010, 01:44 AM
ok so I tried that and it still doesn't work. After filling out the form and pressing submit it opens a new page in a new tab but the page is blank and there is no message saying, "Thank you for your Feedback! You will now be entered into the draw for a pair of gift certificates."
like I would like it to say and that is what I put in the php code above. Then I checked the email account that the form is supposed to be sent to and there is no message, not in the inbox or the junk box. I really don't get what I'm doing wrong and why I can't get a php form to work for me... any other help would be greatly appreciated!

DWcourse
10-26-2010, 01:48 AM
sorry try parentheses rather than brackets:

mail($webMaster, $emailSubject, $body, $headers);

andy1212
10-26-2010, 02:35 AM
So I changed the curly brackets to parenthesis and I also noticed I had added curly brackets behind each post section like this... $_Post['email'}; so I changed each one to this $_Post['email'];
So the curly bracket for each is changed to a regular closing bracket. I uploaded the new file to my host and tested the form, and the exact same things as before happened. It doesn't want to work...

edbr
10-26-2010, 03:00 AM
ive seen this mailform before and problems have been tackled here on this forum. in anycase i would think about a new form that includes some validation , even captcha images as the spam issue can get pretty annoying.

andy1212
10-26-2010, 04:24 AM
Thanks for the help, is there any tutorials that offer a solid php script I could use for my survey form that you could point me to? If so that would be awesome cause I've looked around and can't seem to find one that works. I'll check the previous threads that people posted with the same problem I'm having but if you have links for those that woould be great too. Thanks for all the help though.

andy1212
10-26-2010, 06:29 AM
Ok i've found the tutorials you mentioned above about validation and finding a different scripting method. Thanks again for the help.