PHP Form Validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
techkid
Forum Commoner
Posts: 54
Joined: Sat Sep 05, 2009 11:18 pm
Location: Maldives

PHP Form Validation

Post by techkid »

This is a Form Validation Script which I wrote. But this didn't work. Any help on PHP Form Validation?

Code: Select all

 
<?php
 
error_reporting( 0 );
 
if ($_POST ['submit'] )
{
    //get form data
    $name = $_POST ["name"];
    
    $errorstring = ""; //default value of errorstring
    
    if (!$name)
    $errorstring = $errorstring."Name<br>";
    
    if ($errorstring!="")
    echo "You cannot leave the following fields empty:<br>$errorstring";
    
    else
    {
        //execute
        echo "Success";
    }
    
    
}
 
?>
<form action="formvalidation.php" method="POST">
<input name="name" type="text" id="name" size="25" maxlength="25">
<input name="add" type="submit" id="add" value="Submit">
</form>
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: PHP Form Validation

Post by Weiry »

There were a few problems which was causing it not to work.
  • Your form contained no field named "submit" when you were checking for it on line 6
  • Line 6 was updated to: if(!isset($_POST ['submit']))
  • Line 31 was updated to: <input name="submit" type="submit" id="add" value="Submit">
  • Another point, you were using if statements without formatted statements inside. If you use a non { } if statement, tab it
    Each if statement was reflected to include { }
    Rather than check if the error string == "", i replace with empty() which covers all forms of 'nothing' - empty() function
Here is the updated code:

Code: Select all

<?php
error_reporting(E_ALL);
 
if(!isset($_POST ['submit']))
{
    //get form data
    $name = $_POST ["name"];
   
    $errorstring = ""; //default value of errorstring
   
    if(empty($name)){
        $errorstring = $errorstring."Name<br>";
    }
   
    if (!empty($errorstring)){
        print "You cannot leave the following fields empty:<br>{$errorstring}";
    }else{
        //execute
        echo "Success";
    }
}
?>
<form action="simpleFormValidation.php" method="POST">
<input name="name" type="text" id="name" size="25" maxlength="25">
<input name="submit" type="submit" id="add" value="Submit">
</form>
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: PHP Form Validation

Post by jackpf »

Why turn off error reporting when you're trying to debug :/
User avatar
angelicodin
Forum Commoner
Posts: 81
Joined: Fri Nov 13, 2009 3:17 am
Location: Oregon, USA

Re: PHP Form Validation

Post by angelicodin »

OMG Weiry, you are my hero, I've been wanting a function like this and didn't even knew it existed. Snap and thanks.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: PHP Form Validation

Post by Weiry »

angelicodin wrote:I've been wanting a function like this and didn't even knew it existed
You mean the form validation script?
When you think about it, it can be rather inefficient using a string to hold your errors. Especially if you have more than a single error.
I have been working on an Error class of my own which is supposed to handle errors quite easily.
Example:

Code: Select all

# Set an error to the specified field.
$error->setFieldError("username",ERROR_USER); // where ERROR_USER is a defined numerical value
// The numerical value CURRENTLY relates to a specified array of known errors.
// For instance, my ERROR_USER is defined as 2, which in the error array located in the error class is:
// $errorArray[2] = "Error: Username"
 
# Output error if an error for the field exists
if($error->value("username")){
    print $error->printError("username");
}
If you'd like, PM me and i can send you a copy of the code i currently have. You would probably need the database class as well for it to work properly.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: PHP Form Validation

Post by jackpf »

Just interested, how do you specify what kind of validation you want to run on a field?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

With $error->setFieldError("username",ERROR_USER); :razz:
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: PHP Form Validation

Post by jackpf »

That doesn't specify how you want it to be validated though.

You don't want to validate a username the same way you want to validate a phone number, or a DOB.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

No no, I didn't mean that. I mean, you check the input however you want, and if it's invalid, you call setFieldError.

Of course, this is all pure speculation.
User avatar
angelicodin
Forum Commoner
Posts: 81
Joined: Fri Nov 13, 2009 3:17 am
Location: Oregon, USA

Re: PHP Form Validation

Post by angelicodin »

Weiry wrote:You mean the form validation script?
LOL no, the empty() php function. ;p
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP Form Validation

Post by Jonah Bron »

So, we got off on some wild tangent for nothing!?

Well, it was fun while it lasted :)
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re:

Post by Weiry »

Jonah Bron wrote:Of course, this is all pure speculation.
No speculation, thats exactly how you use it. After all it is an 'error' class not a 'validation' class :D
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: PHP Form Validation

Post by jackpf »

Oh, I guess I was mistaken. My bad.
Post Reply