By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,729 Members | 1,386 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,729 IT Pros & Developers. It's quick & easy.

PHP & Javascript problem

P: n/a
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);
}

Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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 <da****@dlpage.com> wrote in message news:<3f***********************@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);
}

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.