Jump to content

[SOLVED] Simple form validation


herghost

Recommended Posts

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

Link to comment
Share on other sites

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";

}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.