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