Page 1 of 1

PHP Required Fields

Posted: Fri Oct 10, 2008 9:04 pm
by 1234567890
I know this is "easy", and I've been allover the internet trying to make this work but can't, so I am hoping someone with more PHP knowledge than I can help me with this. I just want all my fields required, but how? The code is below:

The form itself:

Code: Select all

<form action="insert.php" method="post">
<div id="header"><h1>IRW Fall 2008 Questionaire</h1></div>
 
<div id="content">
 
<br /><div id="questions">Your Name:</div> <input type="text" name="name" />
<br /><br />
<div id="questions">Your email: </div><input type="text" name="email" />
 
<br /><br />
 
<div id="questions">Age:</div><br />
<input type="radio" name="age" value="18 - 21"> 18 - 21
<br>
<input type="radio" name="age" value="21 - 26"> 21 - 36
<br>
<input type="radio" name="age" value="37 - 54"> 37 - 54
<br>
<input type="radio" name="age" value="55 - 64"> 55 - 64
<br>
<input type="radio" name="age" value="65 or above"> 65 and above
 
<br /><br />
 
<div id="questions">Gender:</div><br />
<input type="radio" name="sex" value="Male"> Male
<br>
<input type="radio" name="sex" value="Female"> Female
 
 
<br /><br />
<div id="questions">Household Income:</div><br />
<input type="radio" name="income" value="$40,000 and under"> $40,000 and under
<br>
<input type="radio" name="income" value="$40,001 - $75,000"> $40,001 - $75,000
<br>
<input type="radio" name="income" value="$75,001 - $125,000"> $75,001 - $125,000
<br>
<input type="radio" name="income" value="$125,001 - $200,000"> $125,001 - $200,000
<br>
<input type="radio" name="income" value="$200,001 and above"> $200,001 and above
 
 
 
 
<br /><br />
<div id="questions">Education Level:</div><br />
<input type="radio" name="edu" value="High School Graduate"> High School Graduate
<br>
<input type="radio" name="edu" value="Some College"> Some College
<br>
<input type="radio" name="edu" value="College Graduate"> College Graduate
<br>
<input type="radio" name="edu" value="Graduate School"> Graduate School
 
<br /><br />
<div id="questions">Children in Home:</div><br />
 
<input type="radio" name="children" value="Yes"> Yes
 
<br>
<input type="radio" name="children" value="No"> No
 
<br /><br />
<div id="questions">How much have you spent on the last 3 months on apparel, accessories and beauty:</div><br />
<br>
<input type="radio" name="apparel" value="$2,500 and under"> $2,500 and under
<br>
<input type="radio" name="apparel" value="$2,500 - $5,000"> $2,500 - $5,000
<br>
<input type="radio" name="apparel" value="$5,000 - $10,000"> $5,000 - $10,000
<br>
<input type="radio" name="apparel" value="$10,000 - $15,000"> $10,000 - $15,000
<br>
<input type="radio" name="apparel" value="$15,000 - $20,000"> $15,000 - $20,000
<br>
<input type="radio" name="apparel" value="More than $20,000"> More than $20,000
 
<input name="Date" type="hidden" id="Date" value="<?=date("F j, Y");?>">
 
<p class="submit"><input type="submit"/></p>
And this is the insert.php file:

Code: Select all

<?php
$con = mysql_connect("host","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
 
 
mysql_select_db("my_db", $con);
$sql="INSERT INTO Questions (Name, Email, Age, Sex, Income, Edu, Children, Apparel, Date)
VALUES
('$_POST[name]','$_POST[email]','$_POST[age]','$_POST[sex]','$_POST[income]','$_POST[edu]','$_POST[children]','$_POST[apparel]','$_POST[Date]')";
 
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
 
echo "Thank you for registering for Independent Retail Week: NYC.
 
You will receive an email shortly with your free 20% coupon.
 
Enjoy the discounts, fashion and beauty.
 
- The Nolcha Team";
 
$subject = 'Free 20% coupon for Independent Retail Week: NYC';
$message = 'Message ';
 
$header = "From: IRW.com";
 
mail($_POST['email'],$subject,$message,$header);
 
mysql_close($con)
?>
Please, any help would be appreciated greatly!

Re: PHP Required Fields

Posted: Sat Oct 11, 2008 5:08 am
by mattcooper
First off, always try to keep naming conventions consistent; so, if you have a database field called 'Age', make sure your form input is named 'Age' and not 'age'. This makes validation tasks much easier.

Try this, remembering to rename your form fields to match the alterations I've made to your database logic:

Code: Select all

 
<?php
    // first, set up an array of required fields
    $requiredFields = array (
        'Name', 'Email', 'Age', 'Sex', 'Income', 'Edu', 'Children', 'Apparel', 'Date'
    );
    // next, test each $_POST item for a value - using a function that you can reuse ;)
    function validatePostFields(array $requiredFields)
    {
        $errors = array();
        foreach ($requiredFields as $field) {
            if (array_key_exists($field, $_POST)) {
                if (empty($_POST[$field])) {
                    $errors[] = $field;
                }
            }
        }
        return (empty($errors)) ? true : $errors;
    }
 
    // run the function and deal with the result
    $errors = validatePostFields($requiredFields);
 
    if (is_array($errors)) {
        // it looks like one or more fields have failed validation
        // redisplay your form with errors (in the $errors array) higlighted
        
    } else {
        // all good, run database logic
        $con = mysql_connect("host","user","pass");
        if (!$con) {
          die('Could not connect: ' . mysql_error());
        }    
        mysql_select_db("my_db", $con);
        $sql="INSERT INTO Questions (Name, Email, Age, Sex, Income, Edu, Children, Apparel, Date)
        VALUES
        ('$_POST["Name"]','$_POST["Email"]','$_POST["Age"]','$_POST["Sex"]','$_POST["Income"]','$_POST["Edu"]','$_POST["Children"]','$_POST["Apparel"]','$_POST["Date"]')";
         
        if (!mysql_query($sql,$con))
        {
        die('Error: ' . mysql_error());
        }
         
        echo "Thank you for registering for Independent Retail Week: NYC.
         
        You will receive an email shortly with your free 20% coupon.
         
        Enjoy the discounts, fashion and beauty.
         
        - The Nolcha Team";
         
        $subject = 'Free 20% coupon for Independent Retail Week: NYC';
        $message = 'Message ';
         
        $header = "From: IRW.com";
         
        mail($_POST['email'],$subject,$message,$header);
         
        mysql_close($con)
    }
?> 
 

Re: PHP Required Fields

Posted: Sat Oct 11, 2008 12:20 pm
by aditya2071990
Here is what I understood from your question:

"You want to make sure that users submit all the form fields and don't leave anything out". Is that right?

If it is, then a simple isset() check will do. For example you have a variable "age" that came out of a form, like this:

Code: Select all

$age = $_POST['age'];
Then in order to make sure it was submitted and not left blank you have to do this:

Code: Select all

if(!isset($age)){
 
echo 'You have not filled the age field.';
exit();
 
};

Re: PHP Required Fields

Posted: Sat Oct 11, 2008 4:03 pm
by mattcooper
aditya2071990 wrote:If it is, then a simple isset() check will do
But this is a naive approach; the function I supplied above returns an array of all empty $_POST fields that can be used as seen fit: isset() only returns true or false for each test. Doing this your way means a lot more code; I've already been relatively comprehensive in one single function ;)

Re: PHP Required Fields

Posted: Sat Oct 11, 2008 10:26 pm
by aditya2071990
That was just my 2 cents, everyone has a different learning curve, so I thought maybe he would like to see a simpler (though not elegant) solution ;) Cheers!

Re: PHP Required Fields

Posted: Sun Oct 12, 2008 6:42 am
by aceconcepts
That certainly is a nice comprehensive function. However, as you could probably presume, this dude is a beginner and will most likely get completely lost and further perplexed by you function.

I think it's always best to offer optional methods. Ideally your very neat function would be most suited.

A lot of the time i find that experienced programmers lose touch with the novices.

Anyway, neat function :D