I have done server side validation for my contact form and when the user fails to enter information into a required field an error occurs when the send button is pressed and no email is sent. I can't get the email to send if all the relevant information is input into the fields. The form is validated and sent on the same page. I have added the right script to send the email but on page refresh it sends a blank email and shows no errors.
<?php
// define variables and set to empty values
$firstnameErr = $secondnameErr = $emailaddressErr = $commentErr = $captchaErr = "";
$firstname = $email = $secondname = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["firstname"])) {
$firstnameErr = "First name is required";
} else {
$firstname = test_input($_POST["firstname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
$firstnameErr = "Invalid first name";
}
}
if (empty($_POST["secondname"])) {
$secondnameErr = "Second name is required";
} else {
$secondname = test_input($_POST["secondname"]);
// check if e-mail address syntax is valid
if (!preg_match("/^[a-zA-Z ]*$/",$secondname)) {
$secondnameErr = "Invalid second name";
}
}
if (empty($_POST["emailaddress"])) {
$emailaddressErr = "Email address is required";
} else {
$emailaddress = test_input($_POST["emailaddress"]);
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$emailaddress)) {
$emailaddressErr = "Invalid email format";
}
}
if (empty($_POST["comment"])) {
$commentErr = "Enter a message";
} else {
$comment = test_input($_POST["comment"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$comment)) {
$commentErr = "Only letters and white space allowed";
}
}
if (empty($_POST["captcha"])) {
$captchaErr = "Enter the answer to the sum";
} else {
$captcha = test_input($_POST["captcha"]);
// check if name only contains letters and whitespace
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form name="Contact" form id="Contact" onsubmit=" return validate()" METHOD="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="Row">
<div class="Lable">First Name:</div> <!--End of Lable-->
<div class="input">
<input type="text" id="firstname" class="detail" name="firstname" placeholder="First Name" />
<span class="error"><?php echo $firstnameErr;?></span> </div>
<!--End input-->
</div> <!--End row--><br />
<div class="Row">
<div class="Lable">Second Name:</div> <!--End of Lable-->
<div class="input">
<input type="text" id="secondname" class="detail" name="secondname" placeholder="Second Name" />
<span class="error"><?php echo $secondnameErr;?></span> </div>
<!--End input-->
</div> <!--End row-->
<br />
<div class="Row">
<div class="Lable">Email Address:</div> <!--End of Lable-->
<div class="input">
<input type="email" id="emailaddress" class="detail" name="emailaddress" placeholder="Email Address" />
<span class="error"><?php echo $emailaddressErr;?></span>
</div> <!--End input-->
</div> <!--End row-->
<br />
<div class="Row">
<div class="Lable">Your Message:</div> <!--End of Lable-->
<div class="input">
<textarea id="comment" name="comment" class="mess" placeholder="Your Message" minlength="10" ></textarea>
<span class="error"><?php echo $commentErr;?></span>
</div> <!--End input-->
</div> <!--End row-->
<br />
<input id="number1" name="number1" readonly="readonly" class="Add" value="<?php echo rand(1,4) ?>" /> +
<input id="number2" name="number2" readonly="readonly" class="Add" value="<?php echo rand(5,9) ?>" /> =
<input type="text" name="captcha" id="captcha" class="captcha" maxlength="2" />
<div class="Lable">Please give the correct answer to the sum</div>
<br />
<span class="captchaerror"><?php echo $captchaErr;?></span>
<br />
<br />
<div class="submit">
<input type="submit" id="send" Name="send" value="Send" />
</div><!--End of submit-->
<div class="Clear">
<input type="reset" id="clear" Name="Clear" value="Clear" />
</div>
</form>
<?php
if (!empty($_POST)) {
}
else {
$firstname = $_POST["firstname"];
$secondname = $_POST["secondname"];
$email = $_POST["emailaddress"];
$comments = $_POST["comment"];
$message = "New Email for a customer" .
"\r\nName of the contact" .
"\r\n-". $firstname .
"\r\nName of the contact" .
"\r\n-". $secondname .
"\r\nEmail address of the contact" .
"\r\n-".$email .
"\r\nThe comment that the contact has made" .
"\r\n-".$comments .
$headers = "From: " . $email;
mail("kieran@localhost",$subject,$message,$headers);
$subjectReply = 'Thank you for your contact..';
$messageReply = 'You will soon receive an answer';
$headers = 'From: admin@shreddednutrition';
mail($email, $subjectReply, $messageReply, $headers);
}
?>
thanks in advance
Why:
if (!empty($_POST)) {
}
else {
so basically the code does nothing if there is some $_POST data..
Change to:
if (empty($_POST)) {
}
else {
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.