Jump to content

Problem with a function


postbil.com

Recommended Posts

Hello Phofreaks

Now I have a new problem. I had created a date validation function after the script has been validated by the day, I will create a timestamp. If I print the time stamp to the screen directly from the function it works fine, but if I print it from my html form, I get nothing. how can it be? And in my role, I have some error messages, but if I make a mistake, do not I get an error. Is there someone who can explain to me what is going wrong??

 

Thank postbil.com

 

Here is my function [function.php]

<?php

    function validateDate($date, $tsDate, $errors){

 

        if(!preg_match('|^\d{2}-\d{2}-\d{4}$|', $date)){

            $errors[] = urlencode('error date. dd-mm-yyy');

        }else{

            list($day, $month, $year) = explode('-', $date);

            if(!checkdate($month, $day, $year)){

                $errors[] = urlencode('error date. dd-mm-yyy');   

            }else{

                $tsDate = mktime(0, 0, 0, $month, $day, $year);

                echo $errors;

                echo $tsDate;

            }

        }     

    }

?>

 

And here is my html form [send.php]

 

<?php

    include ('function.php');

 

?>

<html>

    <head>

    </head>

    <body>

        <form action="send.php" method="post">

        <input type="text" name="date" >

        <input type="submit" name="submit" value="send">

<?php

    if(isset($_POST['submit']) && $_POST['submit'] == 'send' ){

        global $tsDate;

        $date = isset($_POST['date']) ? trim($_POST['date']) : '';

        validateDate($date, $errors, $tsDate);

        echo $tsDate;     

        echo $errors;

    } 

?> 

 

Link to comment
Share on other sites

Not that it matters cause you only use $date but the order does not match between the function call and the function itself.

 

You need return something from the function

return $tsDate;

Then you need to ask for something when you call the function

$tsdate=validateDate($date);

echo $tsdate;

 

If you desire to return both the data and errors you will need to put them into an array then return the array

 

 

HTH

Teamatomic

Link to comment
Share on other sites

    validateDate($date, $errors, $tsDate);

        echo $tsDate;     

        echo $errors;

 

This is your problem.  The arguments passed to the function are not modified by the function.

 

What you would need to do is return values from your function.

 

Here is what you want:

 function validateDate($date){

        if(!preg_match('|^\d{2}-\d{2}-\d{4}$|', $date)){
            $errors[] = urlencode('error date. dd-mm-yyy');
        }else{
            list($day, $month, $year) = explode('-', $date);
            if(!checkdate($month, $day, $year)){
                $errors[] = urlencode('error date. dd-mm-yyy');    
            }else{
                $tsDate = mktime(0, 0, 0, $month, $day, $year);
                
                 
            }
        $tmpArray=array($tsDate, $errors);
         return $tmpArray;
        }       
    }

 

    if(isset($_POST['submit']) && $_POST['submit'] == 'send' ){
        global $tsDate;
        $date = isset($_POST['date']) ? trim($_POST['date']) : '';
        $dateAndError=validateDate($date);
$tsDate=$dateAndError[0];
$errors=$dateAndError[1];
        echo $tsDate;      
        echo $errors;
    }  

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.