Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2010
    Posts
    127
    Thanks
    9
    Thanked 0 Times in 0 Posts

    validation error

    Hi there,

    I would like to validate my form, if there are errors as per the requirement I set therefore the form couldn't proceed and it will show display the error. If no errors, then the form will process to result.php

    Below is my php code:
    PHP Code:
        $error '';    
        
    $day  $_POST['day'];
        
    $month $_POST['month'];
        
    $year  $_POST['year'];
        if(isset(
    $_POST['submit'])){
            if(empty(
    $day)){
                
    $day_error "You need to fill in the day field.";
            } 
            if ((
    $day 0) && $day 31){
                
    $day_error2 "Only 1 to 31 is allowed.";
            }
            if(empty(
    $month)){
                
    $month_error "You need to fill in the month field.";
            } 
            if ((
    $month 0) && $month 12){
                
    $month_error2 "Only 1 to 12 is allowed.";
            }
            if(empty(
    $year)){
                
    $year_error "You need to fill in the year field.";
            } 
            if ((
    $year 1940) && $year 2021){
                
    $year_error2 "Only 1940 to 2021 is allowed.";
            } 
            if(
    $day_error=="" && $day_error2=="" && $month_error=="" && $month_error2=="" && $year_error=="" && $year_error2==""){
                
    // if nothing error
            
    } else { //if error
               
    $error $day_error."<br />".
                  
    $day_error2."<br />".
                  
    $month_error."<br />".
                  
    $month_error2."<br />".
                  
    $year_error."<br />".
                  
    $year_error2."<br />";
            }        
        } 
    and below is the form code:

    PHP Code:
        <form  method="post" action="result.php"  id="searchform">
        <?php echo $error?> 
        <div class="container">
                <span class="label">Date:</span> <input type="text" id="day" name="day" maxlength="2" placeholder="DD" /><br /><br />
                <span class="label">Month:</span> <input type="text" id="month" name="month" maxlength="2" placeholder="MM" /><br /><br />
                <span class="label">Year:</span> <input type="text" id="year" name="year" maxlength="4" placeholder="YYYY" /><br /><br />
            <input type="checkbox" id="dob_chi" name="dob_chi" /><span>Chinese Date of Birth</span><br /><br />
            <input  type="submit" name="submit" value="Search" />
        </div>
        </form>
    Now my current code show me this error:

    Code:
    Notice: Undefined index: day in C:\xampp\htdocs\jeff\index.php on line 2
    
    Notice: Undefined index: month in C:\xampp\htdocs\jeff\index.php on line 3
    
    Notice: Undefined index: year in C:\xampp\htdocs\jeff\index.php on line 4
    
    Notice: Undefined variable: error in C:\xampp\htdocs\jeff\index.php on line 78
    May I know where did I did wrong and making the form not working as per the logic I set?

    Thanks in advance.
    Last edited by lazyserv; 12-28-2013 at 09:11 PM. Reason: amend the code

  • #2
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,444
    Thanks
    11
    Thanked 308 Times in 307 Posts
    Those indexes into $_POST array need to correspond with html ID's. So you need to put an id="day" or whatever into your <input> tags. I suspect you're not actually using the "name=" values, so you can probably get rid of those.

  • #3
    Regular Coder
    Join Date
    Jan 2010
    Posts
    127
    Thanks
    9
    Thanked 0 Times in 0 Posts
    edited the code, but the validation is not working, may I know where did I did wrong?

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    The first three errors are because you have put part of your form processing logic before and outside of your form processing code. Those three $_POST variables don't exist until after your form has been submitted and those three lines of code should inside the block of code that starts with the - if(isset($_POST['submit'])){ statement, i.e. inside your form processing logic.

    The last error is because the $error variable optionally exists. It only exists after your form has been submitted and only if there were any validation errors detected. To prevent that error message, you need to test if it exists before trying to echo it.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Also you shouldn't move the values out of the $_POST array into separate fields until after you have validated them.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

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