Need help with required fields in my form mail - please
Posted: Sun Sep 14, 2008 9:44 pm
Hey guys first post. I have a form set the way I want to use it except I cannot figure out how to make the fields required. I've google searched and searched the forum and I keep thinking that I have found what I need but every time I try to change the code to require fields (with the snippets I find online) the form just gets messed up. This is just some code that my web host suggests. I know nearly nothing about PHP (but I'm trying).
Any idea of how I can make this form require all fields?
formmail.php ------>
<?php
session_start();
$_SESSION['random_number']=rand(1000, 9999);
?>
<html>
<head>
<title>Formmail</title>
</head>
<body>
<!-------------begin form------------>
<FORM ACTION="mail.php" METHOD="POST" NAME="contact_form">
<TABLE>
<TR>
<TD><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Name:</font></TD>
<TD><input type=text name="contact_name"></TD>
</TR>
<TR>
<TD><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Email Address:</font></TD>
<TD><input type=text name="contact_email"></TD>
</TR>
<TR>
<TD><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Comments:</font></TD>
<TD><textarea name="comment" cols="40" rows="3"></textarea></TD>
</TR>
<tr>
<td>
<font size="2" face="Verdana, Arial, Helvetica, sans-serif">Please enter the digits you see below: </font>
</td>
<TD><input type=text name="val_code"></TD>
</tr>
<tr>
<td><img src="image_number.php?n=<?=rand(1000, 9999)?>" alt="number" /></td>
</tr>
<TR>
<TD><input type="reset" value="Reset" name="Reset"></TD>
<TD><input type="submit" value="Submit" name="Submit"></TD>
</TR>
</TABLE>
</FORM>
<!-------------end form------------>
</body>
</html>
Mail.php------>
<?
session_start();
function checkOK($field)
{
if (eregi("\r",$field)){
die("Invalid Input!");
}
}
$val_code=$_POST['val_code'];
$name=$_POST['contact_name'];
checkOK($name);
$phone=$_POST['contact_tel'];
checkOK($phone);
$email=$_POST['contact_email'];
checkOK($email);
$comments=$_POST['comment'];
checkOK($comments);
$to="name.mywebsite.com"; //my email address here
if ("{$_SESSION['random_number']}"!="{$val_code}") {
echo "Wrong Validation Code. Try again.";
}
else {
$message="$name just filled in your comments form. They said:\n$comments\n\nTheir e-mail address was: $email\nTheir phone number was: $phone";
if(mail($to,"From: $email", "Comments From Your Site",$message)) {
echo "Thanks for your comments.";
} else {
echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
}
}
?>
image_number.php------>
<?php
session_start();
header('Pragma: no-cache');
header('Expires: -1');
header('Cache-control: no-cache');
$number=$_SESSION['random_number'];
//$number=123;
header ("Content-type: image/png");
$im = @imagecreatetruecolor(60, 19)
or die("Cannot Initialize new GD image stream");
$text_color = imagecolorallocate($im, 199, 224, 212);
$text_color1 = imagecolorallocate($im, 0, 0, 0);
$background = imagecolorallocate($im, 121, 179, 11);
$e=imagefilledrectangle ( $im, 0, 0,100, 50, $background);
imagestring($im, 8, 2, 2, $number, $text_color);
imagestring($im, 8, 4, 4, $number, $text_color1);
imagegif($im);
imagedestroy($im);
?>
Any idea of how I can make this form require all fields?
formmail.php ------>
<?php
session_start();
$_SESSION['random_number']=rand(1000, 9999);
?>
<html>
<head>
<title>Formmail</title>
</head>
<body>
<!-------------begin form------------>
<FORM ACTION="mail.php" METHOD="POST" NAME="contact_form">
<TABLE>
<TR>
<TD><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Name:</font></TD>
<TD><input type=text name="contact_name"></TD>
</TR>
<TR>
<TD><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Email Address:</font></TD>
<TD><input type=text name="contact_email"></TD>
</TR>
<TR>
<TD><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Comments:</font></TD>
<TD><textarea name="comment" cols="40" rows="3"></textarea></TD>
</TR>
<tr>
<td>
<font size="2" face="Verdana, Arial, Helvetica, sans-serif">Please enter the digits you see below: </font>
</td>
<TD><input type=text name="val_code"></TD>
</tr>
<tr>
<td><img src="image_number.php?n=<?=rand(1000, 9999)?>" alt="number" /></td>
</tr>
<TR>
<TD><input type="reset" value="Reset" name="Reset"></TD>
<TD><input type="submit" value="Submit" name="Submit"></TD>
</TR>
</TABLE>
</FORM>
<!-------------end form------------>
</body>
</html>
Mail.php------>
<?
session_start();
function checkOK($field)
{
if (eregi("\r",$field)){
die("Invalid Input!");
}
}
$val_code=$_POST['val_code'];
$name=$_POST['contact_name'];
checkOK($name);
$phone=$_POST['contact_tel'];
checkOK($phone);
$email=$_POST['contact_email'];
checkOK($email);
$comments=$_POST['comment'];
checkOK($comments);
$to="name.mywebsite.com"; //my email address here
if ("{$_SESSION['random_number']}"!="{$val_code}") {
echo "Wrong Validation Code. Try again.";
}
else {
$message="$name just filled in your comments form. They said:\n$comments\n\nTheir e-mail address was: $email\nTheir phone number was: $phone";
if(mail($to,"From: $email", "Comments From Your Site",$message)) {
echo "Thanks for your comments.";
} else {
echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
}
}
?>
image_number.php------>
<?php
session_start();
header('Pragma: no-cache');
header('Expires: -1');
header('Cache-control: no-cache');
$number=$_SESSION['random_number'];
//$number=123;
header ("Content-type: image/png");
$im = @imagecreatetruecolor(60, 19)
or die("Cannot Initialize new GD image stream");
$text_color = imagecolorallocate($im, 199, 224, 212);
$text_color1 = imagecolorallocate($im, 0, 0, 0);
$background = imagecolorallocate($im, 121, 179, 11);
$e=imagefilledrectangle ( $im, 0, 0,100, 50, $background);
imagestring($im, 8, 2, 2, $number, $text_color);
imagestring($im, 8, 4, 4, $number, $text_color1);
imagegif($im);
imagedestroy($im);
?>