douwulu2576 2017-06-26 22:53
浏览 23
已采纳

PHP复选框分配给变量

This is what I am after here. If the checkbox is checked, then I want to assign these values

$begindate = '20160101';
$enddate = '20161231'

If the checkbox is NOT checked then I want to force the user to select from the date selectors, and if either date is null then throw an error. I have this syntax, and the issue I am having with it is that if the check box is checked AND the user inputs dates then the input dates over-write the checkbox dates. NOT what I want to happen. There may be other syntax flaws in here that my new self has not realized yet, but what needs to be changed in this syntax in order to make it flow properly and execute as I describe above?

    <body>
<form>
Display 2016 Data? &emsp; <input type = 'Checkbox' Name='twentysix' value="twentysix"><br>
<input type="submit" name="submit" value="Gather">
</form>
</body>
<?php
if (isset($_POST['submit'])) 
{
    if ( isset($_POST['twentysix']) ) {
        $begindate = '20160101';
        $enddate = '20161231'
    } else {
        $begindate = $begindate;
        $enddate = enddate;
    }
    $begindateerror = false;
    $enddateerror = false;
    if (empty($_POST['begindate'])) {
        $begindateerror = true;
    }
    if (empty($_POST['enddate'])) {
        $enddateerror = true;
    }
    if ($begindateerror) {
    echo "<strong>Please select a start date.</strong><br>";
    } else if ($enddateerror) {
        echo "<strong>Please select a end date.</strong><br>";
    } else {
        $begindate = $_POST['begindate'];
        $enddate = $_POST['enddate'];
    }
}
  • 写回答

1条回答 默认 最新

  • douzhaxian1267 2017-06-27 02:31
    关注

    Move your if statements for the checkbox to the end

    $begindateerror = false;
    $enddateerror = false;
    if (empty($_POST['begindate'])) {
        $begindateerror = true;
    }
    if (empty($_POST['enddate'])) {
        $enddateerror = true;
    }
    if ($begindateerror) {
    echo "<strong>Please select a start date.</strong><br>";
    } else if ($enddateerror) {
        echo "<strong>Please select a end date.</strong><br>";
    } else {
        $begindate = $_POST['begindate'];
        $enddate = $_POST['enddate'];
    }  
    if ( isset($_POST['twentysix']) ) {
        $begindate = '20160101';
        $enddate = '20161231'
    } else {
        $begindate = $begindate;
        $enddate = enddate;
    }
    

    Note: This is not the best way to do it. I would recommend moving the begin/end date code into the else statment of the checkbox

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历