elsopi
10-14-2009, 11:53 PM
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
<html>
<head>
<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(nums[i].checked)
{
result+=parseFloat(ntext[i].value);
//items+=nitem[i].value+'\n';
}
}
f.total.value=result;
//if you want to fix to 2 decimal places
//f.answer.value=Number(result).toFixed(2);
//f.allitems.value=items;
}
</script>
</head>
<body>
<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"/>
</FORM>
</body>
</html>
the process_payment.php page
<?php
if(isset($_POST['submit'])){
$total = $_POST['total'];
$items = $_POST["item"];
foreach ($items as $eachItems) {
echo $eachItems . "<br>";
}
echo "<b>Total</b>: ". $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
The script i have
<html>
<head>
<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(nums[i].checked)
{
result+=parseFloat(ntext[i].value);
//items+=nitem[i].value+'\n';
}
}
f.total.value=result;
//if you want to fix to 2 decimal places
//f.answer.value=Number(result).toFixed(2);
//f.allitems.value=items;
}
</script>
</head>
<body>
<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"/>
</FORM>
</body>
</html>
the process_payment.php page
<?php
if(isset($_POST['submit'])){
$total = $_POST['total'];
$items = $_POST["item"];
foreach ($items as $eachItems) {
echo $eachItems . "<br>";
}
echo "<b>Total</b>: ". $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