...

View Full Version : validation



psha85
06-16-2010, 02:14 AM
Hi-

I don't know much of PHP so I need help with validating 2 text fields in a contact form on my website. Below is the PHP code I currently have (it just sends the results of the form to my email) but I need to know how to validate the "name" and "email" fields. If the user doesn't enter anything then it should bring up an alert preventing them from submitting until they enter a value (and for the "email" field, if it can be checked as valid email ID format then that would be even better). Here is the code... let me know what to add. Thanks!


<?php
$email = $_REQUEST['email'] ;
$message = $_REQUEST['comments'] ;
$name = $_REQUEST['name'] ;

mail( "p_sha85@yahoo.com", "Feedback from: $name",
$message, "From: $email" );
header( "Location: http://www.poojasdesigns.com/thankyou.html" );
?>

Dormilich
06-16-2010, 07:56 AM
to test, whether the a field has been filled either use the empty() (http://php.net/empty) or strlen() (http://php.net/strlen) function.

testing for a valid email format requires testing against a RegEx (preg_match() (http://php.net/preg-match)). there are quite a lot of such RegExes on the net, you should be able to find a suitable one through google.

psha85
06-16-2010, 06:27 PM
Can you show me exactly how to code this? As I said, i don't know PHP and whatever I had done in this code so far was taken from a tutorial website. I have searched for validating, etc on Google but because I don't know PHP, whatever I had put in from those tutorials didn't work. That's why I need someone in this forum to show me exactly what I need to do based on the code I already have.

Fou-Lu
06-16-2010, 06:44 PM
Email can be validated using the new filter_var functions:


<?php
$email = $_REQUEST['email'] ;
$message = $_REQUEST['comments'] ;
$name = $_REQUEST['name'] ;
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
die('Invalid email address');
}

mail( "p_sha85@yahoo.com", "Feedback from: $name",
$message, "From: $email" );
header( "Location: http://www.poojasdesigns.com/thankyou.html" );
?>


You also need to validate the $message, but thats dependant on what you want to allow. You can use strip_tags to remove any provided html code, or you can use htmlspecialchars / htmlentities to translate them into readable but unusable format. Filter_var can also do this, by using a string santiation to remove all high and low (32 >= character <= 127).

psha85
06-16-2010, 06:47 PM
Great, thanks so much for showing me how to do this! And how can I validate the name field to ensure that some value was entered?

Fou-Lu
06-16-2010, 06:52 PM
Same ways as any:


<?php
$email = $_REQUEST['email'] ;
$message = trim($_REQUEST['comments']);
$name = trim($_REQUEST['name']);

// Fix up the name and message. not worried about the email, any badness will be caught anyway
$message = strip_tags($message);
$name = strip_tags($name);

if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
die('Invalid email address');
}
if (empty($name))
{
die('Name cannot be empty.');
}
if (empty($message))
{
die('Message cannot be empty');
}

mail( "p_sha85@yahoo.com", "Feedback from: $name",
$message, "From: $email" );
header( "Location: http://www.poojasdesigns.com/thankyou.html" );
?>

psha85
06-16-2010, 06:59 PM
works perfectly.. thanks! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum