【问题标题】:Form Validation for opening and closing time php表单验证打开和关闭时间php
【发布时间】:2017-06-11 11:23:18
【问题描述】:

我想知道如何设置用户在 9:00 到 20:00 范围内输入首选开始时间和结束时间,但我认为我的代码的问题是变量类型不正确。

这是表单输入:

<div class="form-group">
       <label>Start Time:<br></label>
       <input type="time" name="starttime" class="form-control" value="<?php echo $_POST['starttime']?>" required>
     </div>
     <div class="form-group">
       <label>End Time:<br></label>
       <input type="time" name="endtime" class="form-control" value="<?php echo $_POST['endtime']?>" required>
     </div>

对于将处理数据的 php 代码:

if(isset($_POST['submit'])){
            echo $open = "9:00";   #declaration for the opening time
            echo " - ";
            echo $close = "20:00";  #declaration for the closing time
            echo "<br>";
            echo $starttime = $_POST['starttime']." - ";
            echo $endtime = $_POST['endtime'];


            echo "<br>time elapsed: ".$total_time = $endtime - $starttime;

            if($starttime>$open){
              echo "Restaurant is open at these hours";
            }else{

            #this part validates the time range after the user's preferred time is within the operating hours of the resto
                      if($total_time > 4){
                      echo "<script>alert('Time Range is too large. Max stay is 4 hours')</script>";
                    } elseif($total_time<1) {
                      echo "<script>alert('Time Range is small.')</script>";
                    }else{
                      echo "oks";
                    }
              echo "string";
            }


          }

错误是没有去else语句,我认为是因为解析变量和比较的问题。有什么方法可以转换两个变量以创建大于或小于比较?

【问题讨论】:

  • 您将 echo 与变量设置混合在一起,不建议这样做

标签: php forms validation time


【解决方案1】:

我会尝试在比较之前将字符串变量转换为浮点数。个人的事情,但我不相信 PHP 可以比较字符串中较大的数字。

我会改变

如果($starttime>$open){

到 类似于: starttime = (float)$startime; $open 也是一样。 if($starttime>=$open){

这个答案应该只是一个评论,但我还没有足够的开始。对此感到抱歉。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-29
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 2013-08-19
    • 2010-10-18
    相关资源
    最近更新 更多