Jump to content

Validating dynamic checkboxes


ShaunP1989

Recommended Posts

Hi there I am kind of new to javascript programming and Im having an error with some code. Firstly I output checkboxes, using php, checkboxes are outputed according to what data is in the database. Each checkbox is given a unique name, chkP[count], where count is a numerical value depending on how many checkboxes have been outputed, the first one therefore being called chkP0, followed by chkP1 etc. I then store the total value of count in a hidden field. In my testing it is 7. The problem comes when I try to recover this value in javascript.

 

The html code for the hidden field is:

 

<input type="hidden" name="hidpcount" id="pcount" value="7">

 

I then get the value into javascript using the following javascript code:

 

element = document.getElementById('pcount');

pcount = element.value;

pcount--;

 

Once this code has been executed the variable pcount holds 6.

 

The problem I get is when i try to use this variable in a for loop:

 

for (i = 0; i <= pcount; i++)

{

checkbox = "chkP" + i;

element = document.getElementById(checkbox);

if (element.checked)

{

donecol = true;

}

}

 

The code will reach the start of the for loop, and then instead of running the code in the for loop it will go to the code after the for loop.

 

The code will run if I replace the varibale pcount with the value 6 in the for loop statement. So I am looking with the problem somewhere withing the pcount variable. I hope someone can point me in the right direction as to where the error may lie.

Link to comment
https://forums.phpfreaks.com/topic/42567-validating-dynamic-checkboxes/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.