PDA

View Full Version : Paasing Multiple Array Checkbox Values To Mysql



hemi519
07-15-2012, 03:37 PM
Hi All,

I am struck with checkbox array issue. Below is my code



if(isset($_POST['submit']))
{
$amount = $_POST['customerid'];
$agent = $_POST['name'];
$amount = $_POST['amount'];


// here i want to to update my table based on customerid. I am unable to pass the values in a loop to table


}


<table border = 1 align="center">
<?php
while($result = mysql_fetch_array($query))
{
?>
<tr>
<td bgcolor="#666666">
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="0" />
</td>
<td align="center"><input name="customerid[]" type="text" id="customerid" value="<?php echo $result['customer_id']; ?>"></td>
<td align="center"><input name="name[]" type="text" id="name" value="<?php echo $result['name']; ?>"></td>
<td align="center"><input name="amount[]" type="text" id="amount" value=" "></td>



</tr>

<?php

}
?>
<input type="submit" name="submit" value="Submit" />
</table>


Anyone can help me in this issue.

JShor
07-18-2012, 02:08 PM
Ok, well the problem here is that all of the related form fields with an array in the name must match their respective checkbox. In lamens terms, everything related to a set checkbox must be set, and nothing else.

What would work is passing an iterative count to the parameter of the array to mark which field should be set. Then you can easily get their name by their count.


So your HTML should look like:


<table border = 1 align="center">
<?php
while($result = mysql_fetch_array($query))
{
$c++;
?>
<tr>
<td bgcolor="#666666">
<input name="checkbox[<?php echo $c; ?>]" type="checkbox" id="checkbox[<?php echo $c; ?>]" value="<?php echo $c; ?>" />
</td>
<td align="center"><input name="customerid[<?php echo $c; ?>]" type="text" id="customerid" value="<?php echo $result['customer_id']; ?>"></td>
<td align="center"><input name="name[<?php echo $c; ?>]" type="text" id="name" value="<?php echo $result['name']; ?>"></td>
<td align="center"><input name="amount[<?php echo $c; ?>]" type="text" id="amount" value=" "></td>



</tr>

<?php

}
?>
<input type="submit" name="submit" value="Submit" />
</table>


And your iterative loop should look something like this:


if(isset($_POST['submit']))
{


// here i want to to update my table based on customerid. I am unable to pass the values in a loop to table

foreach($_POST[checkbox] as $chk) {
$amount = $_POST['customerid'][$chk];
$agent = $_POST['name'][$chk];
$amount = $_POST['amount'][$chk];
}

}


By assigning the value of the checkbox to the current count, and having all of the other fields with the same count, that checkbox can now be associated with those form fields.

hemi519
07-18-2012, 02:48 PM
thank u very much, its working fine now