PHP & Javascript problem

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-26-2003
daniel
 
Posts: n/a
Default PHP & Javascript problem

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);
}

Reply With Quote
  #2 (permalink)  
Old 10-27-2003
Mike Discenza
 
Posts: n/a
Default Re: PHP & Javascript problem

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);
> }

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:43 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0