Roger Godefroy said:
Hi there...
I want to read fieldvalues from out of a dynamicaly created table (php). But
this has to be done by JavaScript. Every row of the table has a select-box,
inputbox and a order-button. So if I press on "order" at row 8, I just want
to popup "Product 8 - Price - Quantity" using alert()corresponding to the
rowvalues from where the button was pressed.
Any help appreciated.
Roger Godefroy
Q-Websolutions.com
Reading values from non-form elements on a web page is possible in newer
browsers (look into .innerText, .innerHTML, and .nodeValue to use these
"newer" features).
However, I would recommend just coding it into a simpler JavaScript at the
head of the document:
....
<html>
....
<head>
<script type="text/javascript">
//Set up the base array for a 2D array.
var tabledata = new Array();
//Array indexes are generated inside PHP loop using loop index.
tabledata[0] = new Array('Product One','Price One');
tabledata[1] = new Array('Product Two','Price Two');
tabledata[2] = new Array('Product Three','Price Three');
//Pop up an alert box with order information.
function popup(row_num) {
//Get quantity from select box.
var quantity = document.getElementById('slctbx'+row_num).value;
//Tell the user what they're ordering:
alert("Order Details:\n\nProduct: "+tabledata[row_num][0]
+"\nPrice: "+tabledata[row_num][1]+"\nQuantity: "
+quantity);
}
</script>
</head>
<body>
<!-- Numbers in form fields are generated
inside PHP loop using loop index. -->
....
<input type="checkbox" name="chkbx0"
value="true" onclick="popup(0);" />
....
<select name="slctbx0">...</select>
....
<input type="checkbox" name="chkbx1"
value="true" onclick="popup(1);" />
....
<select name="slctbx1">...</select>
....
<input type="checkbox" name="chkbx2"
value="true" onclick="popup(2);" />
....
<select name="slctbx2">...</select>
....
</body>
</html>
PHP (and any other server-side execution environment) gives you the ability
to generate dynamic JavaScript. It can save you a lot of hassles that you
encounter when you're trying to put the burden on the visitor's UA (such as
backwards compatibility). Try to keep your JavaScripts very simple (or
non-existent) and leverage PHP's ability to always run the same way no
matter what browser is accessing your page.
HTH,
Zac