php - if statement's not working -
forgive noobness coding teach myself php (lol)!
code below works form want create. whenever user hits submit button, provided form filled in correctly, text "e-mail sent successfully" message appears @ top of page. if of mandatory fields incomplete or invalid, error message returned underneath corresponding input field , "e-mail sent successfully" message not appear. works last if statement (antispam). essentially, if enter correct answer (3) antispam box, regardless of other fields being filled in or not, "e-mail sent successfully" message appears (it shouldn't other fields not filled out correctly). "e-mail sent successfully" message should appear if mandatory fields completed correctly.
whatever 'if statement' goes @ bottom adhere's same rule (i.e. if name @ bottom of if statements, name need entered correctly e-mail sent message appear). feels though if last if statement passed true, 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 valid name"; } if(!preg_match($emailstring,$email_from)) { $error_message_email .= "please enter valid e-mail address"; } if(strlen($comments) < 4) { $error_message_message .= "please enter valid message"; } if($antispam != "3") { $error_message_antispam .= "please enter 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 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 grateful help, i've had several friends , still can't figure out why it's doing this. have tried nesting 'if statements' differently, changing if statement's else if, etc no joy :(
i've looked on forums , can't find solution works me.
you should use variable know if there error (or more one) @ end of verifications. code below give general idea :
$iserror = false; if (!preg_match($namestring, $yourname)) { $error_message_name .= "please enter valid name"; $iserror = true; } if (!preg_match($emailstring, $email_from)) { $error_message_email .= "please enter valid e-mail address"; $iserror = true; } if (strlen($comments) < 4) { $error_message_message .= "please enter valid message"; $iserror = true; } if ($antispam != "3") { $error_message_antispam .= "please enter valid answer"; $iserror = true; } if ($iserror) { //do wathever have } else { //send mail echo "e-mail sent successfully"; }
Comments
Post a Comment