开发者

How to allow user to choose date/time - in PHP only?

I want a complete server side solution, so no JS, etc.

All that I can think of is three combo 开发者_如何学JAVAboxes for day/month/tear, but of course the user can then select an invalid day/month combination.

What's the best way to let use select a date when I am not allowed to use any client side code?


No matter what a user enters into a form you will always have to check that user input. It doesn't matter if you have a fancy JS widget that forces the user to enter valid content. It's quite easy to bypass these on-rails widgets and send any data to the server side.

That said, what I do with dates is use a text input as the form input for a date with the format YYYY-MM-DD. The user can type in a date manually or they can use a date picker that I set up. When they submit the form the PHP de-constructs the input and checks to make sure the date is valid.

$Date = trim($_REQUEST['date']);
$DateError = null;
if (!preg_match("/^(\\d{4,4})-(\\d{2,2})-(\\d{2,2})$/", $Date, $Matches))
{
   $DateError = 'Date must be in the format of YYYY-MM-DD';
}
else
{
    $Year = $Matches[1];
    $Month = $Matches[2];
    $Day = $Matches[3];
    if (!checkdate($Month, $Day, $Year))
    {
        $DateError = 'Invalid date given';
    }
}


Unless you want to list every possible valid date on the page, you'll have to accept that your interface allows the selection of invalid dates, and re-present it with error messages if one of them is chosen.


If you don't want to use any client-side code, you'll have to force the user to select the month, then you'll have to reload the page before allowing them to select the day.

Or you could allow them to choose invalid dates, then kick them back to the form with an error message.


simply return a BIG select box contains from first day to last day

such as 1970-01-01 until ... 2038-01-19 ?

also, something to take note

http://bugs.php.net/44209

http://php.net/ChangeLog-5.php (Look for bug #44209)


Well, yes, if you can't apply any Javascript on the client side, the client can send you anything, even invalid information (note that that's the case even with client side validation). So you need to do your validation on the server, as you would anyway. And since you can't dynamically tell the client that the input is wrong via Javascript, you'll have to go through a POST/validate/GET page reload cycle to tell the client the input was invalid.

There is no better solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜