Page 1 of 1

PHP Form Validation

Posted: Thu Nov 19, 2009 12:13 am
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>

Re: PHP Form Validation

Posted: Thu Nov 19, 2009 12:47 am
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>

Re: PHP Form Validation

Posted: Thu Nov 19, 2009 4:02 am
by jackpf
Why turn off error reporting when you're trying to debug :/

Re: PHP Form Validation

Posted: Thu Nov 19, 2009 5:28 am
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.

Re: PHP Form Validation

Posted: Thu Nov 19, 2009 8:00 am
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.

Re: PHP Form Validation

Posted: Thu Nov 19, 2009 9:27 am
by jackpf
Just interested, how do you specify what kind of validation you want to run on a field?

Posted: Thu Nov 19, 2009 9:35 am
by Jonah Bron
With $error->setFieldError("username",ERROR_USER); :razz:

Re: PHP Form Validation

Posted: Thu Nov 19, 2009 9:36 am
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.

Posted: Thu Nov 19, 2009 10:38 am
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.

Re: PHP Form Validation

Posted: Thu Nov 19, 2009 7:51 pm
by angelicodin
Weiry wrote:You mean the form validation script?
LOL no, the empty() php function. ;p

Re: PHP Form Validation

Posted: Thu Nov 19, 2009 9:23 pm
by Jonah Bron
So, we got off on some wild tangent for nothing!?

Well, it was fun while it lasted :)

Re:

Posted: Fri Nov 20, 2009 2:17 am
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

Re: PHP Form Validation

Posted: Fri Nov 20, 2009 9:41 am
by jackpf
Oh, I guess I was mistaken. My bad.