0

Hi I have this table inside a form and in this form Im also fetching data from a csv file to display product informations in a table. I want a javascript code that when a user inserts a quantity value of the product he wants to buy the total column will automatically compute the total price = Quantity x Price

<?php
//name this functions.php

function displayOrderList()
{
// I ommitted the code for reading CSV File
while (($column = fgetcsv("file.csv",1024)) !== false)
$price = $column[0]; //this is the price i got from the CSV File

echo "
<tr>
<td><input type=text id=qty value=0></td>
<td><input type=text disabled id=price value=$price></td>
<td><input type=text disabled id=total value=0 ></td>
</tr>
}
//this is another php file to display the table
<?php
include ('functions.php');
?>
<html>
<head>
<title>Order Form</title>
</head>
<body>
<!--start of form-->
<form>
<table>
<tr>
<th>Quantity</th>
<th>Price</th>
<th>Total</th>
</tr>
<?php 
      displayProductList();
?>
</table>
<input type=submit value=Order>
</form>
</body>
</html>

There are a lot of missing codes here but I only want to focus on the dynamic table.

I want to know how will I get the values of the Quantity,Price,Total so I can compute the Total and automatically update the Total value using javascript.

Here's a script I have but it only computes the first element.

window.onkeyup=function()
	{
			
			var qty = document.getElementById('qty');
			var price = document.getElementById('price');
			var total = document.getElementById('total');
			
				if (isNaN(qty.value))
				{
					qty.value=qty.value.replace(/\D/g,'');
					alert('Only Numbers Allowed');
				}

				if (qty.value.length>0&&price.value.length>0)
				{
					total.value = parseInt(qty.value) * parseInt(price.value);
				}
	}
4
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by parry_kulk
0

I am not analysed your complete code, but your aim is to get a dynamic table Let take a look into the code to create a dynamic table rows and columns with the filled data from php

echo "<table width = 90% border = '1' cellspacing = '5' cellpadding = '0'>";
    // set variables from form input
    $rows = $_POST['rows'];
    $columns = $_POST['columns'];
    // loop to create rows
    for ($r = 1; $r <= $rows; $r++) {
        echo "<tr>";
        // loop to create columns
        for ($c = 1; $c <= $columns;$c++) {
            echo "<td>&nbsp;</td> ";
        }     echo "</tr> ";
    }
    echo "</table> ";

Hope this helps!

0

Since you are displaying list with

...
<td><input type=text id=qty value=0></td>
...

All the rows will have a text element with id=qty. Same for price and total. As per the specs the id of elements must be unique. Also getElementById() will always fetch the first element with the given id irrespective of there number of occurances.

What I would suggest is to have different ids for different rows in the list like
id=qty0
id=qty1 and so on
and in the function set for onkeyup event send the row number as parameter. Than use this to get the correct elements and compute the totals

0

get the values of the Quantity,Price,Total

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta name="generator" content=
    "HTML Tidy for Windows (vers 25 March 2009), see www.w3.org">
    <script type="text/javascript">
        function computeExtensions() {

            var oTBL = document.getElementById('myTable')
            for (var iTR = 0; iTR < oTBL.rows.length; iTR++) {
                  oTBL.rows[iTR].cells[2].firstChild.data = 
                  oTBL.rows[iTR].cells[0].firstChild.data * oTBL.rows[iTR].cells[1].firstChild.data;
            }
        }
    </script>
    <title></title>
  </head>
  <body>
    <table id="myTable" border="1">
      <tr>
        <td>
          10
        </td>
        <td>
          100
        </td>
        <td>
           
        </td>
      </tr>
      <tr>
        <td>
          20
        </td>
        <td>
          200
        </td>
        <td>
 
        </td>
      </tr>
      <tr>
        <td>
          30
        </td>
        <td>
          300
        </td>
        <td>
           
        </td>
      </tr>
    </table>
    <form>
      <input type="button" onclick="computeExtensions()" value=
      "Compute Extensions">
    </form>
  </body>
</html>

You can call the function to redisplay the table at any convenient time (or, as here, do it all at once [onsubmit, for example]).

Note: there is a small trick here. The TDs for the total are not null (so the .data= assignment works as expected). If those elements are null, use .createTextNode() instead.

0

I think your third nodes are null. Anyway good work. Just add 0 or &nbsp; like below

<table id="myTable" border="1">
      <tr>
        <td>10</td>
        <td>100</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>20</td>
        <td>200</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>30</td>
        <td>300</td>
        <td>&nbsp;</td>
      </tr>
    </table>
0

I think your third nodes are null.

They are not. If they were, my demo would not work.

In the original poster's situation, the nodes in question appear to be generated by php with a value of 0. My cautionary note applies only if those nodes were (or have somehow become) null.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.