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
https://forums.phpfreaks.com/topic/196658-problem-with-a-function/
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

    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;
    }  

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.