needs_upgrade Posted May 9, 2009 Share Posted May 9, 2009 Im sorry guys, im not good in arrays. What if i have this code: <?PHP $sql = "SELECT purchase_id FROM purchases"; $res = mysql_query($sql); while ($row = mysql_fetch_array($res)) { ?> <tr> <td align="center"><input type="checkbox" id="pid[]" name="pid[]" value="<?PHP echo $row[0] ?>" onClick="GetCheckedBoxes()"></td> </tr> <?PHP } How should i do the "GetCheckedBoxes()" function to get the values of the checked checkboxes? Quote Link to comment Share on other sites More sharing options...
Axeia Posted May 9, 2009 Share Posted May 9, 2009 What is getCheckedBoxes supposed to do? If you want to mark it as checked based on a value in $row you should add a checked attribute to the the checkbox and set it to the value checked. ( <input type="checkbox' checked="checked" /> ) And I would advice to use onchange instead of onclick. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 9, 2009 Share Posted May 9, 2009 First of all, you shouldn't have more than *1* element with the same ID on the same page. In your code, you can have multiple <INPUT> tags with ID "pid[]", which is bad. To get all checkboxes, you'll have to write a JavaScript loop that loops through all the checkboxes and check if they're checked. Quote Link to comment Share on other sites More sharing options...
needs_upgrade Posted May 10, 2009 Author Share Posted May 10, 2009 Thanks for your replies. I’m making this function wherein a user can pay their supplier in lump sum. Whenever the user selects/deselects a purchase (that is using checkboxes), the sum of the selected purchases is shown in a readonly textbox. Originally, my plan is to use Ajax. That's why I posted it here: http://www.phpfreaks.com/forums/index.php/topic,250672.0.html. Somebody told me that I can use javascript only to be able to do that. What im trying to do now is somewhat like follows: <form name="form1" method="post" action=""> <?PHP $sql = "SELECT purchase_id, or_num, DATE_FORMAT(delivery_date, '%b %e, %Y'), line_total, balance FROM purchases WHERE supplier_id = '$supplier_id' AND balance > 0 ORDER BY delivery_date DESC, or_num"; $res = mysql_query($sql); $num = mysql_num_rows($res); while ($row = mysql_fetch_array($res)) { ?> <td><input type="checkbox" id="pid[]" name="pid[]" value="<?PHP echo $row[0] ?>" onClick="getCheckedBoxes()"></td> <td><a href="purchase.php?purchase_id=<?PHP echo $row[0] ?>"><?PHP echo $row[1] ?></a></td> <td><?PHP echo $row[2] ?></td> <td><?PHP echo $row[3] ?></td> <td><input type="text" id="balance[]" name="balance[]" value="<?PHP echo $row[4] ?>"</td> <?PHP } ?> <tr class="total"> <td colspan="4"><b>Total Amount</b></td> <td><input type="text" size="20" id="tprice" name="tprice" readonly></td> </tr> <input type="hidden" name="num_rows" value="<?PHP echo $num ?>"> ?> </form> // javascript function getCheckedBoxes() { var num_rows = document.form1.num_rows.value; var totalAmount = 0; for (i = 0; i < num_rows; i++) { if (document.getElementById('pid').checked==true) { var tempSum = eval(parseFloat(document.getElementById('balance').value)); totalAmount =+ tempSum; } } document.form1.tprice.value = totalSum; } Please correct my javascript. I can't get it done. Thanks alot in advanced. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 Click on the Forum DOs link in my sig and read #4. You need to wrap your function in script tags. Quote Link to comment Share on other sites More sharing options...
needs_upgrade Posted May 11, 2009 Author Share Posted May 11, 2009 My apologies ken2k7. Anyway, here it is: Please comment on my javascript if it is correct. The function should get the sum of balances all selected purchases to a particular supplier. <script type="text/javascript"> function getCheckedBoxes() { var num_rows = document.form1.num_rows.value; var totalAmount = 0; for (i = 0; i < num_rows; i++) { if (document.getElementById('pid').checked==true) { var tempSum = eval(parseFloat(document.getElementById('balance').value)); totalAmount =+ tempSum; } } document.form1.tprice.value = totalSum; } </script> <form name="form1" method="post" action=""> <?PHP $sql = "SELECT purchase_id, or_num, DATE_FORMAT(delivery_date, '%b %e, %Y'), line_total, balance FROM purchases WHERE supplier_id = '$supplier_id' AND balance > 0 ORDER BY delivery_date DESC, or_num"; $res = mysql_query($sql); $num = mysql_num_rows($res); while ($row = mysql_fetch_array($res)) { ?> <td><input type="checkbox" id="pid[]" name="pid[]" value="<?PHP echo $row[0] ?>" onClick="getCheckedBoxes()"></td> <td><a href="purchase.php?purchase_id=<?PHP echo $row[0] ?>"><?PHP echo $row[1] ?></a></td> <td><?PHP echo $row[2] ?></td> <td><?PHP echo $row[3] ?></td> <td><input type="text" id="balance[]" name="balance[]" value="<?PHP echo $row[4] ?>"</td> <?PHP } ?> <tr class="total"> <td colspan="4"><b>Total Amount</b></td> <td><input type="text" size="20" id="tprice" name="tprice" readonly></td> </tr> <input type="hidden" name="num_rows" value="<?PHP echo $num ?>"> ?> </form> Thanks in advanced. More power to you guys. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 11, 2009 Share Posted May 11, 2009 That script makes no sense. You're using a for loop where a variable i is set, but you never use it. Also, there is a problem with your HTML. If you view source the page, you would see that there are multiple element with the ID pid[]. An ID should be unique on the page, so you shouldn't have more than 1 element with the same ID. My apologies if that came out a bit harsh. No offense intended. Quote Link to comment Share on other sites More sharing options...
needs_upgrade Posted May 12, 2009 Author Share Posted May 12, 2009 Im sorry, i posted the wrong code. But thank you for your comment ken2k7. Anyway, please check these codes if it makes sense now. <script type="text/javascript"> function getCheckedBoxes() { var num_rows = document.form1.num_rows.value; var totalAmount = 0; for (i = 0; i < num_rows; i++) { if (document.forms["form1"].elements["pid[i]"].checked==true) { var tempSum = eval(parseFloat(document.forms["form1"].elements["balance[i]"].value)); totalAmount =+ tempSum; } } document.form1.tprice.value = totalSum; } </script> <form name="form1" method="post" action=""> <?PHP $sql = "SELECT purchase_id, or_num, DATE_FORMAT(delivery_date, '%b %e, %Y'), line_total, balance FROM purchases WHERE supplier_id = '$supplier_id' AND balance > 0 ORDER BY delivery_date DESC, or_num"; $res = mysql_query($sql); $num = mysql_num_rows($res); while ($row = mysql_fetch_array($res)) { ?> <td><input type="checkbox" name="pid[]" value="<?PHP echo $row[0] ?>" onClick="getCheckedBoxes()"></td> <td><a href="purchase.php?purchase_id=<?PHP echo $row[0] ?>"><?PHP echo $row[1] ?></a></td> <td><?PHP echo $row[2] ?></td> <td><?PHP echo $row[3] ?></td> <td><input type="text" name="balance[]" value="<?PHP echo $row[4] ?>"</td> <?PHP } ?> <tr class="total"> <td colspan="4"><b>Total Amount</b></td> <td><input type="text" size="20" id="tprice" name="tprice" readonly></td> </tr> <input type="hidden" name="num_rows" value="<?PHP echo $num ?>"> ?> </form> Thank you again. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 Now my question is - why do you want to find out what checkboxes are checked with JavaScript? You can do that via PHP too. And much easier. $_POST['pid'] will be an array of all the checkboxes that are checked. Quote Link to comment Share on other sites More sharing options...
needs_upgrade Posted May 13, 2009 Author Share Posted May 13, 2009 I want to find out what checkboxes are checked with JavaScript because I want to show the sum of all checked purchases in the "tprice" read only textbox before submitting the form. Whenever the user selects/deselects a purchase (that is using checkboxes), the sum of the selected purchases is shown in a readonly textbox. That is why i have two arrays, the pic[] which stores the purchase_id's and the balance[] which stores the balance of each purchase. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 Then use something like this: <script type="text/javascript"> function numCheckedBoxes () { var inputs = document.forms.form1.getElementsByTagName("input"); var num_checked = 0; for (var len = inputs.length; --len > -1; ) { if (inputs[len].type == "checkbox" && inputs[len].checked) num_checked++; } window.alert(num_checked); } </script> Use that function. Call it with numCheckedBoxes(); Quote Link to comment Share on other sites More sharing options...
needs_upgrade Posted May 14, 2009 Author Share Posted May 14, 2009 Thanks for your reply Ken2k7. I am going to try your code. Please don't get annoyed but I'm confused. How can i get the sum of selected purchases by just checking/unchecking the textboxes? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.