needs_upgrade Posted May 3, 2009 Share Posted May 3, 2009 Hello guys! I am having difficulty regarding ajax and checkboxes. I’m making this function wherein a user can pay their supplier in lump sum. Whenever the user selects/deselects a purchase, the sum of the selected purchases is shown in a readonly textbox. My problem is that the value in the textbox in incorrect. To be specific, it only gives the “first” row of the query I made, even if it is not selected. I am using the ajaxrequest.js given by alpine found here: http://www.phpfreaks.com/forums/index.php/topic,115581.0.html. I have shortened my php file to be straight to the point. // lump_sum.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="CheckBoxRequest('tprice', 'get_lump_sum.php?pid[]=', 'pid[]')"></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><?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> // get_lump_sum.php $payable = 0; $pid_array = $_GET[pid]; foreach ($pid_array as $purchase_id) { $sql = "SELECT balance FROM purchases WHERE purchase_id = '$purchase_id'"; $res = mysql_query($sql); $row = mysql_fetch_array($res); $payable = $payable + $row[0]; } $payable = number_format($payable, 2); echo $payable; My suspect is that the error is somewhere in this part: <input type="checkbox" id="pid[]" name="pid[]" value="<?PHP echo $row[0] ?>" onClick="CheckBoxRequest('tprice', 'get_lump_sum.php?pid[]=', 'pid[]')"> Or in this part of the get_lump_sum.php: $pid_array = $_GET[pid]; foreach ($pid_array as $purchase_id) { $sql = "SELECT balance FROM purchases WHERE purchase_id = '$purchase_id'"; $res = mysql_query($sql); $row = mysql_fetch_array($res); $payable = $payable + $row[0]; } I have been working on this for more than a week now and I cannot get it right. Any help will be highly appreciated. Thanks in advanced. Quote Link to comment Share on other sites More sharing options...
the182guy Posted May 3, 2009 Share Posted May 3, 2009 You want to list purchases to the user with a checkbox to make a lump sum payment. When a checkbox is ticked it updates the total price to pay in the read only textbox. If I understand this correctly then you don't need AJAX to produce this at all. Don't overcomplicate things by implementing AJAX for this. All you need to do is set a javascript price variable for each product, then have a js function that will just total up the purchases that are ticked. Quote Link to comment Share on other sites More sharing options...
needs_upgrade Posted May 4, 2009 Author Share Posted May 4, 2009 Thanks the182guy for you suggested. But im not familiar on how to pass values from php to javascript as variables. Would you be kind to show me an example on how to do that? I hope it's ok with you. Thanks alot. Quote Link to comment Share on other sites More sharing options...
djbuddhi Posted May 4, 2009 Share Posted May 4, 2009 u have 2 write a function 2 pass the checkbox value 2 php via java script here is a function for this function CheckStaste() { if(document.myform.srcctc.checked != true) { document.myform.srcctc.value=0; } } call this function on click even of the checkbox ....i face the same trouble like when i doing some application....hope this will help ....if not pm with ur code Quote Link to comment Share on other sites More sharing options...
the182guy Posted May 4, 2009 Share Posted May 4, 2009 To send a PHP variable to a javascript variable you can use something like: <html> <head> <script type="text/javascript"> var myData = Array(); //create the blank array variable //now add values to the array from PHP <?php echo " myData[0] = '" . $variable1 . "';"; echo " myData[1] = '" . $variable2 . "';"; echo " myData[2] = '" . $variable3 . "';"; ?> </script> </head> <body> ..document body... </body> </html> Do you see how it is done, all it does is echoes the PHP variables into the javascript array. You can then access the data from javascript to do whatever you want, such as add them up to create a total. Quote Link to comment Share on other sites More sharing options...
needs_upgrade Posted May 5, 2009 Author Share Posted May 5, 2009 Thanks a lot for your help dude! I will try that asap. Quote Link to comment Share on other sites More sharing options...
needs_upgrade Posted May 5, 2009 Author Share Posted May 5, 2009 Thanks a lot for your replies. Please take note that the number of rows returned by the query varies, so also the number of variables in javascript. Please pardon me i dont know how to sum up indefinite number of variables. Quote Link to comment Share on other sites More sharing options...
djbuddhi Posted May 6, 2009 Share Posted May 6, 2009 i have PM u the code .... 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.