Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how can i display selected checkbox values from database

    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
    Code:
    <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
    Code:
    <?php
    	if(isset($_POST['submit'])){
    		$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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    '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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    help please. anyone


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •