dongwen1935 2017-07-18 17:36
浏览 26
已采纳

仅在选中框时发送当前日期

I have a crud system in PHP and creating an approval section. I've create an approval page for the products which works great if the box is checked. But if its not and the person puts in the form, the date is still inputed. How can I have it only input the date if the box is checked?

Here is the form...

// if the form was submitted
if($_POST){

    $product->reviewed = $_POST['reviewed'];
    $product->review_date = $_POST['review_date'];
    // set product property values

    // update the product
    if($product->approve()){
        echo "<div class='alert alert-success alert-dismissable'>";
            echo "Product approvals were set.";
        echo "</div>";
    }

    // if unable to update the product, tell the user
    else{
        echo "<div class='alert alert-danger alert-dismissable'>";
        echo "Unable to approve product.";
        echo "</div>";
    }
}


?>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"] . "?id={$id}");?>" method="post">
    <table class='table table-hover table-responsive table-bordered'>
        <tr>
            <td>Name</td>
            <td><?php echo $product->name; ?></td>
        </tr>
        <tr>
            <td>Review</td>
            <td><?php 
                if (is_null($product->reviewed)){   
                    ?>  
                <div id="ck-button"><label><input type="checkbox" name="reviewed"  value="<?php echo $userRow['user_name'];?>" id="checkbox"/><span>Approve</span>
                    <input type='hidden' name='review_date' class='form-control' value="<?php echo date("Y-m-d h:i:sa"); ?>"/>
                </div>  </label>            

                <?php       ;
                }elseif (isset($product->reviewed)){
                    echo "<div id='approved-button'>" . $product->reviewed . ", " . $product->review_date . "</div>";

                }
                ?>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <button type="submit" class="btn btn-primary">Set Approvals</button>
            </td>
        </tr>
    </table>
</form>

Here is the function in the Product class

    //approval for products
    function approve(){
        $query = "UPDATE
                    " . $this->table_name . "
                SET
                    reviewed = :reviewed,
                    review_date = :review_date

                WHERE
                    id = :id";
        $stmt = $this->conn->prepare($query);

        $this->id=htmlspecialchars(strip_tags($this->id));  
        $this->reviewed=htmlspecialchars(strip_tags($this->reviewed));
        $this->review_date=htmlspecialchars(strip_tags($this->review_date));    

        $stmt->bindParam(':id', $this->id); 
        $stmt->bindParam(":reviewed", $this->reviewed);
        $stmt->bindParam(":review_date", $this->review_date);

        // execute the query
        if($stmt->execute()){
            return true;
        }

        return false;

    }
  • 写回答

1条回答 默认 最新

  • douhuilin1152 2017-07-18 17:39
    关注

    To check if a checkbox element has been 'checked' in PHP you do the following:

    if(isset($_POST['myCheckbox'])) { ... }
    

    Instead, you have

    if($_POST) { ... }
    

    Which is what your problem seems to be if I am understanding your question correctly.

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

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号