herghost Posted August 2, 2008 Share Posted August 2, 2008 How do I validate certain boxes in a form? Is it possible without java? I have to pages, the 1st being the HTML for the form: <form method="POST" action="mailer.php"> <p> </p> <table width="66%" border="0" align="center"> <tr> <td width="31%">Name:</td> <td width="23%"> </td> <td width="46%"><input type="text" name="name" size="19"></td> </tr> <tr> <td>Email Address:</td> <td> </td> <td><input type="text" name="email" size="19"></td> </tr> <tr> <td>Your Message </td> <td> </td> <td><textarea rows="9" name="message" cols="30"></textarea></td> </tr> <tr> <td>Join Mailing List?</td> <td> </td> <td><input type="checkbox" name="check[]" value="join_yes">Yes</td> </tr><tr> <td> </td> <td> </td> <td> </td> </tr> <tr> <td><div align="center"> <input name="submit" type="image" value="Submit" src="images/send.jpg"> </div></td> <td><div align="center"> <input class="reset_button" type="reset" value="" /> </div></td> <td> </td> </tr> </table> <p><br> <br> </p> <p><br> <br> <br> <br> </p> </form></p> </form> and the second is the send function php file: <?php if(isset($_POST['submit'])) { $to = "test@test.com"; $subject = "Contact Form"; $name_field = $_POST['name']; $email_field = $_POST['email']; $message = $_POST['message']; foreach($_POST['check'] as $value) { $check_msg .= "Checked: $value\n"; } $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message\n $check_msg"; echo "Thankyou for your message, you will now be returned "; mail($to, $subject, $body); } else { echo "blarg!"; } ?> How would I validate that all boxes have been filled (apart from the checkbox) and the email address is in the correct format? Thanks for any help Dave Quote Link to comment Share on other sites More sharing options...
PHPTOM Posted August 3, 2008 Share Posted August 3, 2008 You didn't say if you were using PHP 5 or not. But this is a PHP file email validator in it. <?php if(isset($_POST['submit'])) { $to = "test@test.com"; $subject = "Contact Form"; $name_field = $_POST['name']; $email_field = $_POST['email']; $message = $_POST['message']; if(!$name_field || !$email_field || !$message){ echo "You missed a field!"; }elseif(!filter_var($email_field, FILTER_VALIDATE_EMAIL)) { echo "Invalid email!"; }else{ foreach($_POST['check'] as $value) { $check_msg .= "Checked: $value\n"; } $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message\n $check_msg"; mail($to, $subject, $body); echo "Thankyou for your message, you will now be returned "; } }else{ echo "You didn't press the submit button"; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.