I need help with this multiple action checkbox buttons. The button is designed so that Users can select some check boxes or select all then click any the YES, NO or WAITING buttons which will in turn change the relevant field to Yes, No, Waiting and this works fine. But the issue is that if the checkbox is not checked and Users click any of the buttons (YES, NO or WAITING) by mistake it will still change all the data to Yes, No or waiting. How do I make the YES, NO or WAITING buttons function only when some or all check boxes are checked.
<?php
if(isset($_POST['Yes'])){
$multiple = $_POST['multiple'];
$i = 0;
$sql = "UPDATE productlist SET sold = 'Yes'";
foreach($multiple as $item_id) {$i ++;
if ($i == 1) {
$sql .= " WHERE product_id = ". mysqli_real_escape_string($con_pathway, $item_id) . "";
} else {
$sql .= " OR product_id = ".mysqli_real_escape_string($con_pathway, $item_id)."";
}
}
mysqli_query($con_pathway, $sql) or die(mysqli_error($con_pathway));
header("location: ".$_SERVER['PHP_SELF']);
exit();
}
if(isset($_POST['No'])){
$multiple = $_POST['multiple'];
$i = 0;
$sql = "UPDATE productlist SET sold = 'No'";
foreach($multiple as $item_id) {$i ++;
if ($i == 1) {
$sql .= " WHERE product_id = ". mysqli_real_escape_string($con_pathway, $item_id) . "";
} else {
$sql .= " OR product_id = ".mysqli_real_escape_string($con_pathway, $item_id)."";
}
}
mysqli_query($con_pathway, $sql) or die(mysqli_error($con_pathway));
header("location: ".$_SERVER['PHP_SELF']);
exit();
}
if(isset($_POST['Waiting'])){
$multiple = $_POST['multiple'];
$i = 0;
$sql = "UPDATE productlist SET sold = 'Waiting'";
foreach($multiple as $item_id) {$i ++;
if ($i == 1) {
$sql .= " WHERE product_id = ". mysqli_real_escape_string($con_pathway, $item_id) . "";
} else {
$sql .= " OR product_id = ".mysqli_real_escape_string($con_pathway, $item_id)."";
}
}
mysqli_query($con_pathway, $sql) or die(mysqli_error($con_pathway));
header("location: ".$_SERVER['PHP_SELF']);
exit();
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="GET">
<table width="20%" border="0" align="center" cellpadding="1" cellspacing="1">
<tr class="one">
<td width="65%"></td>
<td></td>
<td></td>
</tr>
</table>
<table width="70%" border="1" align="center" cellpadding="0" cellspacing="0">
<tr align="center" valign="middle" class="one">
<td> </td>
<td><input type="submit" name="Yes" value="Yes"></td>
<td><input type="submit" name="No" value="No"></td>
<td><input type="submit" name="Waiting" value="Waiting"></td>
<td> </td>
</tr>
<tr align="center" valign="middle" class="one">
<td>
<div><strong>SelectAll</strong></div>
<input type="checkbox" name="day1" id="day1">
</td>
<td><em><strong>Product ID</strong></em></td>
<td><em>Product Name</em></td>
<td><em><strong>Sold</strong></em></td>
<td><strong><em>Active</em></strong></td>
</tr>
<?php do { ?>
<tr >
<td align="center" valign="middle"><strong>
<input type="checkbox" name="multiple[]" value="<?php echo $row_productlist['product_id']; ?>">
</strong></td>
<td><strong><?php echo $row_productlist['product_id']; ?></strong></td>
<td><strong><?php echo $row_productlist['name']; ?></strong></td>
<td><strong><?php echo $row_productlist['sold']; ?></strong></td>
<td><strong><?php echo $row_productlist['status']; ?></strong></td>
</tr>
<?php } while ($row_productlist = mysqli_fetch_assoc($productlist)); ?>
</table>
</form><!--close form-->
<script type="text/javascript">
var day1 = document.getElementById('day1');
day1.onclick = function () {
var multiple = document.getElementsByName('multiple[]');
for (i = 0; i < multiple.length; i ++) {
multiple[i].checked = this.checked;
}
}
</script>
How do I make the YES, NO or WAITING buttons function only when some or all check boxes are checked. Thank you
Aucun commentaire:
Enregistrer un commentaire