Forgive my noobness coding as I teach myself PHP (lol)!
The code below works almost perfectly for a form I want to create. Whenever the user hits the submit button, provided the form is all filled in correctly, the text "E-mail Sent Successfully" message appears at the top of the page. If any of the mandatory fields are incomplete or invalid, then an error message is returned underneath the corresponding input field and the "e-mail sent successfully" message does not appear. This works properly for all but the last if statement (antispam). Essentially, if you enter the correct answer (3) into the antispam box, then regardless of the other fields being filled in or not, the "e-mail Sent Successfully" message appears (it shouldn't do this as other fields are not filled out correctly). The "e-mail Sent Successfully" message should only appear if all the mandatory fields are completed correctly.
Whatever 'if statement' goes at the bottom adhere's to the same rule (i.e. if name was at the bottom of the if statements, then only name would need to be entered correctly for the e-mail sent successfully message to appear). It feels as though if the last if statement is passed as true, the other statements don't work.
<?php
if (isset($_POST['submit'])) {
$yourname = $_POST['yourname'];
$organisation = $_POST['organisation'];
$email_from = $_POST['email'];
$comments = $_POST['comments'];
$antispam = $_POST['antispam'];
$namestring = "/^[A-Za-z .'-]+$/";
$emailstring = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
$error_message_email = "";
$error_message_name = "";
$error_message_message = "";
$error_message_antispam = "";
if(!preg_match($namestring,$yourname)) {
$error_message_name .= "Please enter a valid name";
}
if(!preg_match($emailstring,$email_from)) {
$error_message_email .= "Please enter a valid e-mail address";
}
if(strlen($comments) < 4) {
$error_message_message .= "Please enter a valid message";
}
if($antispam != "3") {
$error_message_antispam .= "Please enter a valid answer";
}
else {
echo "E-mail Sent Successfully";
}
}
else {
$yourname="";
$organisation="";
$email_from="";
$comments="";
$antispam="";
$error_message_email = "";
$error_message_name = "";
$error_message_message = "";
$error_message_antispam = "";
}
?>
<form name="htmlform" method="post" action="ifandelse.php">
<table width="650px">
<tr>
<td>
<label for="yourname"><font color="#ff0000">*</font>Name:</label>
</td>
<td>
<input type="text" name="yourname" value="<?PHP echo$yourname;?>" maxlength="50" size="40"><br>
<div class="warning">
<?php print $error_message_name;?>
</div>
</td>
</tr>
<tr>
<td>
<label for="organisation">Organisation:</label>
</td>
<td>
<input type="text" name="organisation" value="<?PHP echo$organisation;?>"maxlength="50" size="40">
</td>
</tr>
<tr>
<td>
<label for="email"><font color="#ff0000">*</font>E-mail Address:</label>
</td>
<td>
<input type="text" name="email" value="<?PHP echo$email_from;?>" maxlength="80" size="40"><br>
<div class="warning">
<?php print $error_message_email;?>
</div>
</td>
</tr>
<tr>
<td valign="top">
<label for="comments"><font color="#ff0000">*</font>Message:<br></label>
</td>
<td>
<textarea name="comments" rows="8" cols="60"><?PHP echo$comments;?></textarea><br>
<div class="warning">
<?php print $error_message_message;?>
</div>
</td>
</tr>
<tr>
<td colspan="2">
<font color="#ff0000">*</font>To avoid unwanted spam, please answer the following question correctly: 2 + 1 = <input type="text" name="antispam" value="<?PHP echo$antispam?>" maxlength="100" style="width:30px"><br>
<div class="warning">
<?php print $error_message_antispam;?>
</div>
</td>
</tr>
<tr>
<td>
</td>
<td style="text-align:center;">
<input type="submit" class="formsubmit" name="submit" value="Submit Form">
</td>
</tr>
</table>
</form>
Be most grateful for any help, I've had several friends look into this and we still can't figure out why it's doing this. Have tried nesting the 'if statements' differently, changing the if statement's to else if, etc with no joy :(
I've looked on the forums and can't find a solution that works for me.
You should use a variable to know if there was an error (or more than one) at the end of your verifications. Some code below to give you the general idea :
$isError = false;
if (!preg_match($namestring, $yourname)) {
$error_message_name .= "Please enter a valid name";
$isError = true;
}
if (!preg_match($emailstring, $email_from)) {
$error_message_email .= "Please enter a valid e-mail address";
$isError = true;
}
if (strlen($comments) < 4) {
$error_message_message .= "Please enter a valid message";
$isError = true;
}
if ($antispam != "3") {
$error_message_antispam .= "Please enter a valid answer";
$isError = true;
}
if ($isError) {
//do wathever you have to do
} else {
//send mail
echo "E-mail Sent Successfully";
}
The else statement is attached to you last if and only to this one. Hence, only the last condition matters regarding whether your success message is displayed or not. You'll have to chain your checks :
if (!preg_match($namestring, $yourname)) {
$error_message_name .= "Please enter a valid name";
}
elseif (!preg_match($emailstring, $email_from)) {
$error_message_email .= "Please enter a valid e-mail address";
}
elseif (strlen($comments) < 4) {
$error_message_message .= "Please enter a valid message";
}
elseif ($antispam != "3") {
$error_message_antispam .= "Please enter a valid answer";
} else {
echo "E-mail Sent Successfully";
}
Of course, this will still leave you displaying only one error message at a time, which is far from optimized but that's another matter.