I set up a form on an html site using a form I found on the net. The form worked beautifully and sent me an email with the submitted content, so we know it works and the server can handle php and mail etc.
Then I changed some fields on the page the form is on, and tried to edit the contact.php part to match it, and of course the form stopped sending email. So, I've done something wrong but have no idea what exactly to change on the contact.php side or contact.html side to make it work again.
Also, is this form at least somewhat secure or is there something else I should be adding to it? This is the first form I have ever set up, and I will be eternally grateful for some help.
PLEASE NOTE: I did actually use the real email and not you@domain.com on the real site. Also, please if you would point it out as if to a dummy. I'm as old as Methuselah and don't understand this php stuff at all, but am attempting to learn.
The original contact.php:
<?php
/* Set e-mail recipient */
$myemail = "you@domain.com";
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$subject = check_input($_POST['subject'], "Write a subject");
$email = check_input($_POST['email']);
$website = check_input($_POST['website']);
$likeit = check_input($_POST['likeit']);
$how_find = check_input($_POST['how']);
$comments = check_input($_POST['comments'], "Write your comments");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
$website = '';
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Name: $yourname
E-mail: $email
URL: $website
Like the website? $likeit
How did he/she find it? $how_find
Comments:
$comments
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
The edited contact.php that stopped sending mail:
<?php
/* Set e-mail recipient */
$myemail = "me@domain.com";
/* Check all form inputs using check_input function */
$firstname = check_input($_POST['firstname'], "Enter your first name");
$lastname = check_input($_POST['lastname'], "Enter your last name");
$email = check_input($_POST['email'], "Enter your email");
$phone = check_input($_POST['phone'], "Enter your phone");
/*$how_time = check_input($_POST['time']);*/
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
First Name: $firstname
Last Name: $lastname
E-mail: $email
Best time to call: $how_time
End of message
";
/* Send the message using mail() function */
mail($myemail, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
The original form on the html page:
<form action="contact.php" method="post">
<p><b>Your Name:</b> <input type="text" name="yourname" /><br />
<b>Subject:</b> <input type="text" name="subject" /><br />
<b>E-mail:</b> <input type="text" name="email" /><br />
Website: <input type="text" name="website"></p>
<p>Do you like this website?
<input type="radio" name="likeit" value="Yes" checked="checked" /> Yes
<input type="radio" name="likeit" value="No" /> No
<input type="radio" name="likeit" value="Not sure" /> Not sure</p>
<p>How did you find us?
<select name="how">
<option value=""> -- Please select -- </option>
<option>Google</option>
<option>Yahoo</option>
<option>Link from a website</option>
<option>Word of mouth</option>
<option>Other</option>
</select>
<p><b>Your comments:</b><br />
<textarea name="comments" rows="10" cols="40"></textarea></p>
<p><input type="submit" value="Send it!"></p>
<p> </p>
<p>Powered by <a href="http://myphpform.com">PHP form</a></p>
</form>
The edited html form:
<form action="contact.php" method="post">
<p>
<table>
<tr class="label"><td>First Name</td><td>Last Name</td></tr>
<tr>
<td><input type="text" name="firstname" /></td>
<td><input type="text" name="lastname" /></td>
</tr>
<tr class="label"><td colspan="2">Email Address</td></tr>
<tr>
<td colspan="2" class="one-line">
<input type="text" name="email" /></td></tr>
<tr class="label"><td>Phone Number</td><td>Best time to call? (optional)</td></tr>
<tr>
<td>
<input type="text" name="phone" /></td>
<td>
<select name="time">
<option value="">- Please select -</option>
<option>Morning (8am - Noon)</option>
<option>Afternoon (Noon - 5pm)</option>
<option>Evening (After 5pm)</option>
</select>
</td>
</tr>
<tr class="last-row">
<td><span class="small"> </span></td>
<td><input type="submit" value="Get a Call Back"/></td>
</tr>
</table>
</form>
I'm sorry this is so long and involved, but I am at my wits end.
Thank you!
NOTE: In case someone else experiences this, the problem was two items instead of three in this:
mail($myemail, $message);
I added "subject" to that and it worked, even though there is no subject field on the form itself. Thank you so much to whoever suggested that - I cannot find your original answer now to thank you in your thread. I would NEVER have guessed it, such a simple thing and saved so much time and frustration. One does not know what one does not know.