0

hi, am recieving an undefined offset error for $month and $year line after the explode function. This happens only when the form is blank and submitted.

 $dob="dd/mm/yyyy";
 $dob= $_POST['dob'];   

function icPrintINPUTbirth($label,$name,$maxlth,$size,$value)
                {
                print '<label>'.$label.':<input type="text" name="'.$name.'" 
                maxlength="'.$maxlth.'" size="'.$size.'" value="'.$value.'"/></label>'.'';
                }

 icPrintINPUTbirth("Date of Birth (dd/mm/yyyy) ","dob",10,10,$dob); 


        $arr=explode("/","$dob"); 
                    $day=$arr[0];
                    $month=$arr[1];
                    $year=$arr[2];

     if(@checkdate($month, $day, $year)){
                    print ''; 
                }else{
                    $errs[]= "The date entered is not valid, please try again </br>";
                    }

CHeers

4
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by pritaeas
0

why are you running a function without validating the data first? You can try something like this.
Here's a resource: http://stackoverflow.com/questions/2086598/validate-date-format-in-php?answertab=votes#tab-top

function validDate($date) {
    if (substr_count($date, '/') == 2) {
        list($day, $month, $year) = explode('/', $date);
        return checkdate($month, $day, sprintf('%04u', $year));
    }
    return false;
}

$date = false;
if(!empty($_POST['date']))$date = validDate($_POST['date']);

if($date){
    $d = $_POST['date'];
    echo "<label>Date of Birth (dd/mm/yyyy) :<input type=\"text\" name=\"dob\" maxlength=\"10\" size=\"10\" value="\$d\" /></label>";
}else{
    $errs[]= "The date entered is not valid, please try again </br>";
}

I just took the function apart as it looked like it was a one shot - not designed to be re-used.

Edited by diafol

-1

Change this

 $dob= $_POST['dob'];  

to

if(isset($_POST['dob']))  $dob = $_POST['dob'];
 $dob = 'xx/xx/xxxx';
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.