This is a discussion on PHP & Javascript problem within the PHP Language forums, part of the PHP Programming Forums category; Hi, This is more a javascript than a PHP problem, but anyway, if anyone can help.... I have a script ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi,
This is more a javascript than a PHP problem, but anyway, if anyone can help.... I have a script that will dynamically create a series of checkboxes according to the number of values in the database (a pricelist). The checkboxes are created with a naming convention from cb1 to cbx (x being the number of records returned). Each checkbox calls the onclick action, calling a unique custom javascript function called add(). How it should work: add() takes the name of the clicked checkbox (provided as an argument), checks to see if the checkbox is checked or not, and then, depending on the state, adds the value of the checkbox (a price) to the value in a textbox. Problem: If I hardcode the checkbox name, I can do this without any problems, but I cannot know in advance how many checkboxes will be created... but it seems that I cannot use a variable as part of the DOM call... Is there a solution or a workaround? Cheers, Daniel For example: This works... function add() { //This will work as cb1 is hardcoded. alert (document.test.cb1.value); } This does not.... function add(cbname) { //Here, this will display "cb1". alert(cbname); //But here, cbname is taken as a DOM object, and not a variable. //and raises an error. alert (document.test.cbname.value); } |
|
|||
|
There is a very simple way to do this. Just name all the checkboxes
the same thing (itemPrice). When you generate the list of checkboxes from the database keep track of each one you make. Use a counter to set the index of the checkbox, which is what you will pass to your add() function. <?php $c = 0; while($row = mysql_fetch_row($result)) {echo "<input type=checkbox name=itemPrice onClick="add($c)" value='something'>Field $c</input><br>\n";} ?> Then in your javascript use the number passed as the index for accessing the fields since they are all now an array, and go about your business. js stuff -------- function add(indexNum) { if(indexNum <= document.forms[0].itemPrice.length) {document.forms[0].textField.value = document.forms[0].textField.value + "\n" + document.forms[0].itemPrice[indexNum].value;} else {alert('Item Index out of bounds');} } Now the list is more or less dynamic and you won't have anything to worry about. daniel <daniel@dlpage.com> wrote in message news:<3f9c2bd8$0$13160$79c14f64@nan-newsreader-01.noos.net>... > Hi, > > This is more a javascript than a PHP problem, but anyway, if anyone can > help.... > > I have a script that will dynamically create a series of checkboxes > according to the number of values in the database (a pricelist). > > The checkboxes are created with a naming convention from cb1 to cbx (x > being the number of records returned). > > Each checkbox calls the onclick action, calling a unique custom > javascript function called add(). > > How it should work: add() takes the name of the clicked checkbox > (provided as an argument), checks to see if the checkbox is checked or > not, and then, depending on the state, adds the value of the checkbox (a > price) to the value in a textbox. > > Problem: If I hardcode the checkbox name, I can do this without any > problems, but I cannot know in advance how many checkboxes will be > created... but it seems that I cannot use a variable as part of the DOM > call... > > Is there a solution or a workaround? > > Cheers, > Daniel > > For example: > This works... > > function add() > { > //This will work as cb1 is hardcoded. > alert (document.test.cb1.value); > } > > This does not.... > function add(cbname) > { > //Here, this will display "cb1". > alert(cbname); > > //But here, cbname is taken as a DOM object, and not a variable. > //and raises an error. > alert (document.test.cbname.value); > } |