I'm building an admin form that allows other teams to update aspects of their own data so they don't have to call me to do it for them.
They update the data via checkboxes or text fields. There is also a requirement that they be able to edit multiple rows at once. So the form required them to select which rows they will be updating or have updated. so far so good.
My problem is thus: If the first record has one checkbox selected and the second record has 3 boxes checked, when the form is submitted and the data echoed back out, the first record shows 3 values and the second record shows 1 indicating that record 1 has somehow taken the values from record 2 for itself. I know that's not the case but can't figure out why that data is not staying with record 2.
I've beat around this and searched and found numerous examples that should work but they are just using a single text field and not checkboxes. So, I'm hoping someone here can help me sort it out.
<?php
if(isset($_POST['updateRecord'])) {
// echo "Update Record Outer Check<br>";
if ((strtolower($_POST['updateRecord']) == "update")) {
foreach ($_POST['update'] as $key => $update) {
$id = $_POST['id'][$key];
$regOK = (
isset($_POST['regOK'][$key]) ?
$_POST['regOK'][$key] : false
);
$paidOK = (
isset($_POST['paidOK'][$key]) ?
$_POST['paidOK'][$key] : false
);
$ebdOK = (
isset($_POST['ebdOK'][$key]) ?
$_POST['ebdOK'][$key] : false
);
$appOK = (
isset($_POST['appOK'][$key]) ?
$_POST['appOK'][$key] : false
);
echo "RECORD ID: ".$id."<br>";
echo " REG: ".$regOK."<br>";
echo "PAID: ".$paidOK."<br>";
echo " EBD: ".$ebdOK."<br>";
echo " APP: ".$appOK."<br><br>";
}
}
}
echo "<br><br>";
?>
<style type="text/css">
th,td {text-align:center;}
</style>
<form action="" method="post" name="theForm">
<table cellpadding="2" cellspacing="0" class="resultsTable">
<tr>
<th>REG</th>
<th>PAID</th>
<th>EBD</th>
<th>APP</th>
<th>Update Record</th>
</tr>
<tr>
<td><input type="checkbox" name="regOK[]" id="regOK" value="1" checked></td>
<td><input type="checkbox" name="paidOK[]" id="paidOK" value="1"></td>
<td><input type="checkbox" name="ebdOK[]" id="ebdOK" value="1"></td>
<td><input type="checkbox" name="appOK[]" id="appOK" value="1"></td>
<td>
<input type="checkbox" class="test" name="update[]" id="update" value="1">
<input type="hidden" name="updateRecord[]" id="updateRecord" value="update">
<input type="hidden" name="id[]" id="id" value="1" />
</td>
</tr>
<tr>
<td><input type="checkbox" name="regOK[]" id="regOK" value="1" checked></td>
<td><input type="checkbox" name="paidOK[]" id="paidOK" value="1"></td>
<td><input type="checkbox" name="ebdOK[]" id="ebdOK" value="1"></td>
<td><input type="checkbox" name="appOK[]" id="appOK" value="1"></td>
<td>
<input type="checkbox" class="test" name="update[]" id="update" value="2">
<input type="hidden" name="updateRecord[]" id="updateRecord" value="update">
<input type="hidden" name="id[]" id="id" value="2" />
</td>
</tr>
<tr>
<td><input type="checkbox" name="regOK[]" id="regOK" value="1"></td>
<td><input type="checkbox" name="paidOK[]" id="paidOK" value="1"></td>
<td><input type="checkbox" name="ebdOK[]" id="ebdOK" value="1"></td>
<td><input type="checkbox" name="appOK[]" id="appOK" value="1"></td>
<td>
<input type="checkbox" name="update[]" id="update" value="3">
<input type="hidden" name="updateRecord" id="updateRecord" value="update">
<input type="hidden" name="id[]" id="id" value="3" />
</td>
</tr>
<tr><td colspan="5" ><input type="submit" name="updateFormData" id="updateFormData" value="Update All Records"></td></tr>
</table>
</form>
The first two checkboxes are checked. Leave row 1 alone and check the second checkbox for record 2, select the update checkbox for the first two rows and then submit the form. What I expect to see is:
RECORD ID: 1 REG: 1 PAID: EBD: APP:
RECORD ID: 2 REG: 1 PAID: 1 EBD: APP:
What I'm getting in this example is: RECORD ID: 1 REG: 1 PAID: 1 EBD: APP:
RECORD ID: 2 REG: 1 PAID: EBD: APP:
You can see where the first record shows the data from row two.
Bug in your code is indexing, handling form with index will solve it
<?php
if(isset($_POST['updateRecord'])) {
// echo "Update Record Outer Check<br>";
if ((strtolower($_POST['updateRecord']) == "update")) {
foreach ($_POST['update'] as $key => $update) {
$id = $_POST['id'][$key];
$regOK = (
isset($_POST['regOK'][$key]) ?
$_POST['regOK'][$key] : false
);
$paidOK = (
isset($_POST['paidOK'][$key]) ?
$_POST['paidOK'][$key] : false
);
$ebdOK = (
isset($_POST['ebdOK'][$key]) ?
$_POST['ebdOK'][$key] : false
);
$appOK = (
isset($_POST['appOK'][$key]) ?
$_POST['appOK'][$key] : false
);
echo "RECORD ID: ".$id."<br>";
echo " REG: ".$regOK."<br>";
echo "PAID: ".$paidOK."<br>";
echo " EBD: ".$ebdOK."<br>";
echo " APP: ".$appOK."<br><br>";
}
}
}
echo "<br><br>";
?>
<style type="text/css">
th,td {text-align:center;}
</style>
<form action="" method="post" name="theForm">
<table cellpadding="2" cellspacing="0" class="resultsTable">
<tr>
<th>REG</th>
<th>PAID</th>
<th>EBD</th>
<th>APP</th>
<th>Update Record</th>
</tr>
<tr>
<td><input type="checkbox" name="regOK[]" id="regOK" value="1" checked></td>
<td><input type="checkbox" name="paidOK[]" id="paidOK" value="1"></td>
<td><input type="checkbox" name="ebdOK[]" id="ebdOK" value="1"></td>
<td><input type="checkbox" name="appOK[]" id="appOK" value="1"></td>
<td>
<input type="checkbox" class="test" name="update[]" id="update" value="1">
<input type="hidden" name="updateRecord[]" id="updateRecord" value="update">
<input type="hidden" name="id[]" id="id" value="1" />
</td>
</tr>
<tr>
<td><input type="checkbox" name="regOK[1]" id="regOK" value="1" checked></td>
<td><input type="checkbox" name="paidOK[1]" id="paidOK" value="1"></td>
<td><input type="checkbox" name="ebdOK[1]" id="ebdOK" value="1"></td>
<td><input type="checkbox" name="appOK[1]" id="appOK" value="1"></td>
<td>
<input type="checkbox" class="test" name="update[1]" id="update" value="2">
<input type="hidden" name="updateRecord[1]" id="updateRecord" value="update">
<input type="hidden" name="id[]" id="id" value="2" />
</td>
</tr>
<tr>
<td><input type="checkbox" name="regOK[2]" id="regOK" value="1"></td>
<td><input type="checkbox" name="paidOK[2]" id="paidOK" value="1"></td>
<td><input type="checkbox" name="ebdOK[2]" id="ebdOK" value="1"></td>
<td><input type="checkbox" name="appOK[2]" id="appOK" value="1"></td>
<td>
<input type="checkbox" name="update[2]" id="update" value="3">
<input type="hidden" name="updateRecord" id="updateRecord" value="update">
<input type="hidden" name="id[]" id="id" value="3" />
</td>
</tr>
<tr><td colspan="5" ><input type="submit" name="updateFormData" id="updateFormData" value="Update All Records"></td></tr>
</table>
</form>
Collected from the Internet
Please contact javaer1[email protected] to delete if infringement.
Comments