I am a PHP newbie. My employer wants an email newsletter signup on our website. I built the form, but it is not validating. I got the script from my server, Host Gator. The script works for posting, but if you leave off the name or email, it just takes you to a blank white page.
I've never seen a form validated with the unset($_GET['do']);
here is the script:
<?php
switch (@$_GET['do'])
{
case "send":
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$femail = $_POST['femail'];
$fsendmail = $_POST['fsendmail'];
$secretinfo = $_POST['info'];
if (!preg_match("/\S+/",$fname))
{
unset($_GET['do']);
$message = "First Name required. Please try again.";
break;
}
if (!preg_match("/\S+/",$lname))
{
unset($_GET['do']);
$message = "Last Name required. Please try again.";
break;
}
if (!preg_match("/^\S+@[A-Za-z0-9_.-]+\.[A-Za-z]{2,6}$/",$femail))
{
unset($_GET['do']);
$message = "Primary Email Address is incorrect. Please try again.";
break;
}
if ($secretinfo == "")
{
$myemail = "newsletter@washingtoncountysheriffwi.org";
$emess = "First Name: ".$fname."\n";
$emess.= "Last Name: ".$lname."\n";
$emess.= "Email: ".$femail."\n";
$emess.= "Comments: ".$fsendmail;
$ehead = "From: ".$femail."\r\n";
$subj = "An Email from ".$fname." ".$lname."!";
$mailsend=mail("$myemail","$subj","$emess","$ehead");
$message = "Email was sent.";
}
unset($_GET['do']);
header("Location: thankyounews.html");
break;
default: break;
}
?>
The form works, but if name or email is omitted, it goes to blank page instead of error notification.
Here is the html:
<form action="email_form.php?do=send" method="POST">
<p>* Required fields</p>
<?php
if($message) echo '<p style="color: #FF0000;">'.$message.'</p>';
?>
<table width="400" border="0">
<tr>
<td align="right">* First Name:</td>
<td><input type="text" name="fname" size="30" value="<?php echo @$fname ?>" /></td>
</tr>
<tr>
<td align="right">* Last Name:</td>
<td><input type="text" name="lname" size="30" value="<?php echo @$lname ?>" /></td>
</tr>
<tr>
<td align="right">* Primary Email:</td>
<td><input type="text" name="femail" size="30" value="<?php echo @$femail ?>" /></td>
</tr>
<tr>
<td align="right"> </td>
<td> </td>
</tr>
<tr>
<td ><TEXTAREA name="fsendmail" ROWS="6" COLS="40"><?php if($fsendmail) echo $fsendmail;
?></TEXTAREA></td>
<tr>
<td align="right"><input type="submit" value="Send Now" />
</td>
</tr>
</table>
</form>
The form works, but it does not validate. Is there a better way to make this work? I tried and echo after the switch, but don't know enough to make it work.
Thanks,
Helen