Jump to content

[SOLVED] form calculations


Kano

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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++;

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.