Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: PHP Form Validation

  1. #1
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default PHP Form Validation

    Ok I know I have I think posted this everywhere except for on my own site, but I really need some help with this... I only got 10 days to get my site finished...
    Ok, I need someone to help me make a validation script or something that will help make sure people won't send in blank forms...
    HTML Code:
    <form action="uploader.php" method="post" enctype="multipart/data">
    <input type="hidden" name="3p42r3ad3a4than32" value="329r8g" /> 
    <div class="pureadd">
    <br>
    <br>Your Name:
    <br><input type="text" name="Name" class="input" />
    <br>Your Email:
    <br><input type="text" name="Email" class="input"/>
    <br>
    <script type="text/javascript">
    var d = new Date()
    document.write("<input type='hidden' value='"+Date()+"' name='time' />")
    </script>
    <br>
    <br>Agree To Terms of Service
    <br><input type="checkbox" name="TermsOfService" value="Agreed" /> Yes I Agree To the Terms of Service
    <br>
    
    File Description:
    <br>
    <textarea rows="5" cols="40" name="description" id="maxcharfield" onKeyDown="textCounter(this,'progressbar1',200)" onKeyUp="textCounter(this,'progressbar1',200)" onFocus="textCounter(this,'progressbar1',200)" class="textarea"></textarea><br />
    <div id="progressbar1" class="progress"></div>
    
    <input type="submit" class="input">
    
    
    </div>
    </form>
    I would like it so either that the form won't submit till the user has filled in all the fields. or some other way that you can do it I would be glad. please someone help me i'm very desperate, I swear if I don't get an answer to this I will have to cancel my site... no kidding... cancel it... PLEASE!!!!!!!!!!!!!!!!!!!!!!!!!!!

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    http://www.dynamicdrive.com/dynamici...uiredcheck.htm


    And/or you could do this server side.

    PHP Code:
    <?php

    function checkvals($list) {
    foreach (
    $list as $var) {
    if (!isset(
    $_POST[$var]) { return FALSE; }
    if (
    $_POST[$var]=='') { return FALSE; }
    }
    return 
    TRUE;
    }
    $vals = array('myfiled1','myotherfield',.....,'mylastfieldname');
    if (!
    checkvals($vals)) { echo 'ERROR'; }

    ?>
    I'm not sure how all/any browsers handle blank fields, so that accounts for either an empty string or a nonexistent value.
    Assuming that they do submit a blank field as blank, you could pass $_POST to the function to check if any of them are blank.

    This might not work on checkboxes, etc, as that would not necessarily be an empty string if left blank. Might want to play with that.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    Thanks man, i'll use the javascript version since it looks unfortunitly more promising...

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    If you use the Javascript version, people can still upload blank forms (people with Javascript disabled won't even notice that it's there). You must have server-side checking as well.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    Ok, then should I use a php validation then *since this thread is called "PHP Form Validation..." But what i'm going to do is have the submit button in a document write script example:
    Code:
    <script type="text/javascript">
    document.write('<input type="submit" class="button" />')
    </script>
    That way inorder for the user with javascript disabled it wouldn't allow them to submit a blank or invalid form...

  6. #6
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by Rockonmetal View Post
    Ok, then should I use a php validation then *since this thread is called "PHP Form Validation..." But what i'm going to do is have the submit button in a document write script example:
    Code:
    <script type="text/javascript">
    document.write('<input type="submit" class="button" />')
    </script>
    That way inorder for the user with javascript disabled it wouldn't allow them to submit a blank or invalid form...
    you don't need to do that and really, your site should be accessible whether they have javascript enabled or disabled. before you do anything else with the content you would sanitize and validate it. if everything passes your would then go ahead and process the information, but if there were required fields left blank you redirect them back to the form and display some type visual means of explaining what error'd out

  7. #7
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    Thats the problem, I DON'T KNOW HOW TO DO THAT... *sorry, but i felt like making it big, sudden urges, followed by doubt...* but anyways could someone write me a validation that would validate the current script no matter whether their browser doesn't support javascript... like i said I HAVE NO IDEA HOW TO DO THAT *again sudden urges to make font big followed by, regret...* but please could someone help me by writing the script or telling me how to add/remove and what most parts do...

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That way inorder for the user with javascript disabled it wouldn't allow them to submit a blank or invalid form...
    It wouldn't let them submit a form at all (from that page, anyway). If they really wanted to send invalid data they could write their own form, enable Javascript then disable the validation with bookmarklets or something, or just send raw POST data, in order of required technical expertise.
    I DON'T KNOW HOW TO DO THAT...
    That depends on the data and what you want to do with it. If you intend to display it in HTML, it needs to be passed through htmlentities(). If you intend to store it in a database, you need to use mysql_real_escape_string(). If it needs to be filled in, you need to pass it to empty() to check it. If it needs to be a number, you need is_numeric()... &c.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    Quote:
    I DON'T KNOW HOW TO DO THAT...

    That depends on the data and what you want to do with it. If you intend to display it in HTML, it needs to be passed through htmlentities(). If you intend to store it in a database, you need to use mysql_real_escape_string(). If it needs to be filled in, you need to pass it to empty() to check it. If it needs to be a number, you need is_numeric()... &c.
    Again I sort of understand but I don't know how to do that... which again, is making me nuts...

    All I ask is for someone to write php code that would check to see if all the inputs aren't empty, and that either a real email address or a possible real email address is there. I am having trouble understanding why people can't understand this... maybe i am not being clear... if so... please tell me... but if you do please help or say something!

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The script I wrote above will do just that.

    The only thing you'd need to customize is the behavior when there is an error.

    For validating an email, you can use preg_match() and a regex that will check the email.

    Just use this line, then:
    if (!checkvals($vals) || !preg_match($_POST['email'],'/^[^@]+@[^.]+(\.[^.]+)+$/')) { echo 'ERROR'; }
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •