View Full Version : How do I make input fields required?

05-21-2011, 05:50 AM
How can I make "Name" and "Phone" required fields in the code below. Any help is appreciated.

if (strpos($email,"@")==0 or strpos($email,".")==0){$email="";}
if ($email!="" and $text!=""){
mail("webmaster@mysite.com","Contact form", $message,"From: <webmaster@mysite.com>\nContent-Type: text/plain");
print "<center>Message Sent!</center>";

<form action=<? print $_SERVER["PHP_SELF"]; ?> method=post>
<INPUT NAME="name" SIZE="35" MAXLENGTH="65">
<INPUT NAME="phone" SIZE="35" MAXLENGTH="65">
<INPUT NAME="email" SIZE="35" MAXLENGTH="65">
Your Message:
<TEXTAREA name=text cols=40 rows=5><? print $text; ?></TEXTAREA>

<? } ?>

05-21-2011, 10:16 AM
if (strpos($email,"@")==0 or strpos($email,".")==0){$email="";}
That's not sufficient to check the format of email, since as per you logic xx.@ will be a valid email.

You need to use a regular expression to match (http://php.net/manual/en/function.preg-match.php) or filter_var() (http://php.net/manual/en/filter.examples.validation.php)

You may modify your code as per the following pseudo

if(Email format is invalid)
$errors[]="Email is invalid"
if( value of "name" is empty or doesn't have the required length )
$errors[]="Name is invalid/short"

//send email

PS: When using strpos(), you'd need to use === operator to compare the value returned. Check http://php.net/strpos#refsect1-function.strpos-returnvalues

05-21-2011, 09:17 PM
That's not sufficient to check the format of email, since as per you logic xx.@ will be a valid email.

You need to...

I originally had this titled to acknowledge that I'm an absolute beginner with PHP but no one was responding. Should have kept it there cause I'm totally lost. I copied this code from a tutorial and I'm not too concerned with security (the site is for local clients only). Could you point out the code I need to add and remove to make the Name and Phone fields required using the same technique as the code I have? Or at least just so they're sent with the e-mail.

05-22-2011, 11:37 AM
If all you want to do is make them required, you would simply need to check if their values are set or null and act accordingly. Something like this:

//do something; email field is not empty
} else {
//email field is empty; do something else
There's a lot of ways to check if a field is empty. In forms I usually use 'strlen($variable) > x' since form fields usually require a specific length. Basically, for all the fields you want to require, you would want to check if the data they submitted was empty and act accordingly; if they are not empty, submit the form to your database (or wherever the data is being submitted to), and if they are, give an error message. For simplicity, you could do this all on one line.

if(strlen($name) > 0 && strlen($phone) > 0){
echo "Form submission successful!"
} else {
echo "Please fill out all forms."

As far as regex goes (what Abdura was explaining), you shouldn't worry too much about that now if you're just learning PHP. My advice would be to get your form working without regex first and then work on implementing it afterwards.

05-22-2011, 10:17 PM
Say we skip the required part and I just want to have the name and phone sent to my e-mail along with their e-mail address and comments which are already sending properly. I have experimented with this code that's already in the original code:

if ($email!="" and $text!=""){
mail("info@example.com","New Message!", $message,"From: <info@example.com>\nContent-Type: text/plain");

From my minor ActionScript experience I would think this is saying "If the email and text fields aren't empty then send the email and text comments." So I tried adding $name and $phone but couldn't get it to work. I'm asking the code to be implemented into the existing code cause I'm clueless to PHP.

05-22-2011, 10:32 PM
Well $name and $phone are always going to be blank because you haven't assigned them values. You need to set them to the values sent by the forms, which will be done (just like with email and text) using $_POST['fieldname'].

$phone = $_POST['phone'];
$name = $_POST['name'];

Or you could skip that entirely (creating variables with the post values) and simply use the $_POST variables in your statement:

if ($_POST['phone'] != "" && $_POST['name'] != ""){