SitePoint Sponsor

User Tag List

Results 1 to 24 of 24

Thread: PHP Validation

  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Validation

    hi there, I have some forms within my company website which i need to validate. The problem i seem to face is that i have many forms with lots of different sections and i would prefer not making validation for each individual form, and would rather have one script which would (for example) search the $post variables and check if they are empty, and for specific variables i.e. email check they are valid. If someone has some code which does this i would be eternally greatful as i have so much do to and there is a deadline...aarrgghh...thanks

    Rodent

  2. #2
    is_empty(2); foofoonet's Avatar
    Join Date
    Mar 2006
    Posts
    1,000
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This would be an ideal candidate for some validation classes, try phpclasses.org or I think there might be a PEAR class that does similar.

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I cannot use OO PHP as my version we have at work does not support it unfortunately...(as far as i know) anyone have any functional ideas??

    Rodetn

  4. #4
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What PHP version do you have? If its too old, then it might be time to update it, but i can see it can be problem sometimes in companies to update servers like that.
    ---------------------------
    Errors = Improved Programming.
    My Site

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is what i was trying to do...

    PHP Code:
    $Validate extract($_POST);
    foreach (
    $Validate as $key=>$value) {
               if(
    $key == ""){
               unset(
    $_POST['redirect']);
            
    $valid $valid."Please Enter ".$value."<br>";
            }

    I was trying to get the post variable, and then loop through each one, and if it says there is an error then display one...i think i messed it up ??

    Rodent

  6. #6
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well if that how you want it,,
    i think you should change this line
    if($key == "")

    TO
    if($value == "") //because that is what you want to check

    you can also do

    if(is_empty($value)) OR
    if(strlen(trim(value))<=0)

    But, i also suggest that if that is the case then use the FormMail.php script and i think in there you can put what you require and what not. Just a thought
    ---------------------------
    Errors = Improved Programming.
    My Site

  7. #7
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok i have actually gotten here now...

    PHP Code:
    foreach($_POST as $key=>$value){
        if(
    $value == ""){
            
    $valid $valid."Error Missing Field ".$key."<br>";
        }

    Now this works and it displays all of the form fields...now i need to take this to the next level...is there somethign that i can place in all the form fields that are actually required, and then the loop above only asks for the required fields? This would mean that it will only ask for what needs to be filled in.

    The step after this is email validation..but one step at a time...thx for helping

  8. #8
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi to everyone who is helping...i would also like to add another idea how i would do the above problem...if i was to add this following line to the top of my form

    PHP Code:
        <input type="hidden" name="required" value="name,company,position,email,address1,Town,country,postcode,telephone"
    How would i then take this and compare it to the various $key values and only check those that are the same as those listed above in the hidden "required" field..

    I know i would have to extract the $post and then place them into an array to do the check but for the life of me i do not know the way to code or the syntax. If you can help me please let me know

    Rodent

  9. #9
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can make a hidden field and in there put the names of the fields seperated by comma or something
    <input type="hidde" name="required" value="email,firstname,lastname....." />

    then in php you can explode this array and check it
    PHP Code:
    $requiredArr explode($_POSt['required'],',');
    foreach(
    $_POST as $key=>$value){
        if(
    in_array($value,$requiredArr) && is_empty($value)){
            
    $valid $valid."Error Missing Field ".$key."<br>";
        }

    Again, its not the better idea, but will do what you want.

    You can also make more different hidden fields as
    <input type=hidden" name="formatemail" value="senderemail....." />

    Then you do it the same as above, but in this one you know that rather than checking for is_empty, you would check for PROPER EMAIL FORMAT.

    hope this helps
    ---------------------------
    Errors = Improved Programming.
    My Site

  10. #10
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah i see, that i posted right after you.
    ---------------------------
    Errors = Improved Programming.
    My Site

  11. #11
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
        if(in_array($value,$requiredArr) && is_empty($value)){ 
    This line does not seem to work...its not processing the file...any ideas??

    rodent (thx btw)

  12. #12
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do not know if In_array is in a high version of PHP but if it is do you have any other suggestions??

  13. #13
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i am running PHP Version 4.3.9 and this cannot be changed....Rodent

  14. #14
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you just copied and pasted it, it wont work.

    i saw an error there
    $_POSt['required']

    should be
    $_POST['required']
    ---------------------------
    Errors = Improved Programming.
    My Site

  15. #15
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jaswinder_rana
    If you just copied and pasted it, it wont work.

    i saw an error there
    $_POSt['required']

    should be
    $_POST['required']
    already spotted that... still did not work

  16. #16
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh i am sorry
    this line
    in_array($value,$requiredArr)

    should be
    in_array($key,$requiredArr)

    we are checking for key, right.
    ---------------------------
    Errors = Improved Programming.
    My Site

  17. #17
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no...still does not seem to be working...
    PHP Code:

    $requiredArr 
    explode($_POST['required'],',');
    foreach(
    $_POST as $key=>$value){
        if(
    in_array($key,$requiredArr) && $value==""){
            echo 
    "In Array";
            
    $valid $valid."Error Missing Field ".$key."<br>";
        }

    i have a feeling that my php version does not let that function work

    Rodent

  18. #18
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i again ask, what is your PHPversion. if you mention it, then you might get better help. the in_array function is for PHP4 and 5.
    ---------------------------
    Errors = Improved Programming.
    My Site

  19. #19
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok figured out the problem

    PHP Code:
    explode($_POST['required'],','); 
    This is the wrong way round...the delimiter should be first

    PHP Code:
    explode(',',$_POST['required']); 
    Secondly i decided to do it another method now too...rather than going through each value needed to be validated, instead i went through the required field...(more efficient)

    final code

    PHP Code:
    $requiredArr explode(',',$_POST['required']);
    foreach (
    $requiredArr as $field) {
        if(
    $_POST[$field] == ""){
         
    $valid $valid."Error Missing Field ".$field."<br>";
        }

    Thankyou for all your help m8

    Rodent

  20. #20
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah yes, dint test it and dint check syntax before posting. you got it anyways.
    ---------------------------
    Errors = Improved Programming.
    My Site

  21. #21
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol...now all i need to do is check for the email field and then for that field check its a valid email address.....Fun!

  22. #22
    is_empty(2); foofoonet's Avatar
    Join Date
    Mar 2006
    Posts
    1,000
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jaswinder_rana
    You can make a hidden field and in there put the names of the fields seperated by comma or something
    <input type="hidde" name="required" value="email,firstname,lastname....." />
    You might want to check that carefully. I can take a copy of your page, delete everything on this list, and resubmit it to your server.

  23. #23
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I never said that is a foolproof solution. But the original poster wanted a generic solution. So, he has to live with that.

    He doesn't want to check each and everything. He said so in his post
    ---------------------------
    Errors = Improved Programming.
    My Site

  24. #24
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i would recomend defining which fields are required in your script.

    for example

    PHP Code:
    // true means required
    $post_fields = array(
        
    'name' => true,
        
    'email' => true,
        
    'address' => true,
        
    'join_newsletter' => false,
        
    'something_optional' => false
    );


    foreach (
    $post_fields as $key => $required) {
        if (!
    $required) {
            
    // not required, dont generate an error
        
    } elseif (!isset($_POST[$key]) || !strlen($_POST[$key])) {
             
    // either the variable doesnt exist, or it is empty
             // make an error
        
    }



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •