View Full Version : how can i display selected checkbox values from database

10-15-2009, 12:53 AM
I am trying to display the name of items selected by a user from a check box. the values for the checkbox are generated from the database. When a user submits the form, i want to display the name and amount of the selected items and total.
The script i have

<script type="text/javascript">
function calculate(f)
var nums = f.num;
var ntext = f.numtext;
var nitem = f.numitem;
var result = 0;
var items = '';
for(var i=0;i<nums.length;i++)

//if you want to fix to 2 decimal places

<FORM ACTION="process_payment.php" NAME="form1" method="post">
while($row = mysql_fetch_array($result))
echo "<tr>";
echo "<td>" . $row['sb_name'] . "</td>";
echo "<td>" . number_format($row['sb_amount']) . "</td>";
echo "<td><input type='checkbox' name='num' onclick='calculate(this.form)'>
<input type='hidden' name='numtext' value='".$row['sb_amount']."' onchange='calculate(this.form)'>
<input type='hidden' name='item[]' value='".$row['sb_name']."' >";
echo "</tr>";
<input type="text" name="total"/>

the process_payment.php page

$total = $_POST['total'];

$items = $_POST["item"];
foreach ($items as $eachItems) {
echo $eachItems . "<br>";
echo "<b>Total</b>:&nbsp;". $total;


What i get is all the values from the database, I just what the selected item from the form. can someone please help me out here. Thanks

10-15-2009, 03:08 AM
'item' is a hidden field, not a checkbox. Hidden fields are always passed, and since its an array you will end up with every value. You need to alter this so that you're working off of the actual checkbox as an array, not the hidden field.

10-15-2009, 09:26 AM
help please. anyone