Kano Posted March 5, 2007 Share Posted March 5, 2007 Hi there, I was wondering if anyone new of some good scripts that calculate the input (quantity) with the price of the produce. The price of the product is a <td> and the quantity is an <input = 'text>. But, each <td> and <input> is built from a loop in php where it gets the product details, because we don't the exact number of rows on the page how do we calculate each row? I have search the internet and found scripts but none seem to take into account that the form is built using php/MySQL loops. The script is so the use can keep a running total when inputting quantities, Thanks. Quote Link to comment Share on other sites More sharing options...
fenway Posted March 5, 2007 Share Posted March 5, 2007 What do you mean "calculate each row"? Quote Link to comment Share on other sites More sharing options...
Kano Posted March 5, 2007 Author Share Posted March 5, 2007 Hi there, for example each row has the product description including price and an input box, the user inputs the quantity and then we will have another input box which states the total price for that order. Then at the bottom of the order it will have sub totals and totals of the entire order. Thanks. Quote Link to comment Share on other sites More sharing options...
fenway Posted March 5, 2007 Share Posted March 5, 2007 Understood... you just need to have an onblur function for each of those inputs that recalculates both the subtotal and updates the entire total... Quote Link to comment Share on other sites More sharing options...
Kano Posted March 5, 2007 Author Share Posted March 5, 2007 onBlur, thanks. Also, how does Javascript calculate a <td> * <input> thanks. Quote Link to comment Share on other sites More sharing options...
fenway Posted March 5, 2007 Share Posted March 5, 2007 Well, you have two options (the input is easy) -- either write out a hidden input for the price, or put in into a id'ed span so that you can "get at" the number. Quote Link to comment Share on other sites More sharing options...
Kano Posted March 5, 2007 Author Share Posted March 5, 2007 OK <span id= could it just be <td id = And also the javascript, how does it make sure the sub totals get the correct numbers For example, the php that build the form is in a loop and because the number of rows is unknown each id (for the <td> and <input>) will equal a count, so how does javascript do this without the number of rows known? Thanks. Quote Link to comment Share on other sites More sharing options...
fenway Posted March 5, 2007 Share Posted March 5, 2007 Don't put it on the TD, because you may have formatting elements inside... if you place it in a span, you can always be sure that you'll get the "raw" value. As for the subtotals, simply write out appropriate IDs to match with your INPUTs (i.e. similar prefixes/suffixes) so that you can find them again. As for the grand total, you (the php script) obviously know how many rows there are, so include that logic into the JS code. Quote Link to comment Share on other sites More sharing options...
Kano Posted March 5, 2007 Author Share Posted March 5, 2007 OK I understand. The grand total is easy but because I want each sub total to mulitply the quantity by the price as the user inputs the quantity how does javascript know how many rows there are? Here is some javascript: function calc() { var elements = document.getElementsByTagName("input"); for(var i = 0; i < elements.length; i++) { if(elements.type == 'text') { if(elements.id.indexOf("qty") != '-1') { document.form.qty????????????.value = (document.form.****span id*****.value) * (document.form.*****qty input box.value) } Question: How can I get javascript to include after the qty, the number of the count? (where the ??????? are) Thanks. Quote Link to comment Share on other sites More sharing options...
fenway Posted March 5, 2007 Share Posted March 5, 2007 First, you should be going through the form's elements collection, not all tags on the page... Second, how do you decide this in PHP? Quote Link to comment Share on other sites More sharing options...
Kano Posted March 5, 2007 Author Share Posted March 5, 2007 Here is the php that builds the form: $sqlqry_getwsi = "select * from tbl_wsi"; $sqlres_getwsi = mysql_query($sqlqry_getwsi) or die ("Could not execute getwsi query"); $num = 1; while($row_getwsi = mysql_fetch_array($sqlres_getwsi)) { extract($row_getwsi); echo "<tr><td>$wsi_tn</td><td>$wsi_id</td><td>$wsi_desc</td><td>$wsi_dim</td><td>$wsi_cm</td><td>$wsi_price</td><span id = 'A$num'><td>$wsi_qty</td></span><td>$wsi_nextshiparr</td><td><input type='text' value='$remember_qty[$num]' id='A$num' name='$wsi_id' /></td><td><input = 'text' name='total$num' onchange='calc()' /></td></tr>"; $num++; } Quote Link to comment Share on other sites More sharing options...
fenway Posted March 5, 2007 Share Posted March 5, 2007 OK... well, you have a $num variable, so simply use this in your name/ids... then, in your function that figures out the total, simply tell to iterate though $num rows. Quote Link to comment Share on other sites More sharing options...
Kano Posted March 5, 2007 Author Share Posted March 5, 2007 Thank-you for the discusion. I think we have got somewhere, many thanks. 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.