Connecting Tech Pros Worldwide Forums | Help | Site Map

Creating checkboxes dynamically document.createElement

Member
 
Join Date: Jul 2007
Posts: 69
#1: Aug 31 '07
Hi,

I've put together a little test page to create checkboxes each time a button is clicked. The check button is created and should be appended to the form, but when the post is submitted the dynamic checkboxes are not available in $_POST in PHP.

Here's the code:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.    <head>
  3.    <script language="Javascript">
  4.  
  5.      function append()
  6.      {
  7.         var cb = document.createElement( "input" );
  8.         cb.type = "checkbox";
  9.         cb.id = "id";
  10.         cb.value = "test";
  11.         cb.checked = true;
  12.         var text = document.createTextNode( "checkbox" );
  13.         document.getElementById( 'append' ).appendChild( text );
  14.         document.getElementById( 'append' ).appendChild( cb );
  15.      }
  16.  
  17.    </script>
  18.    </head>
  19. <body>
  20.    <p>click the button below</p>
  21.    <form action="http://localhost/test.php" name="form" id="form" method="post" enctype="multipart/form-data">
  22.    <div id="append" name="append">Append here</div>
  23.    <input type="button" value="append" onclick="javascript:append()" />
  24.    <input type="submit" value="submit" />
  25.    </form>
  26. </body>
  27. </html>
  28.  
Also is there a way to view the updated DOM tree at runtime? Viewing the page source doesn't show me what I've created at runtime.

Thanks,

Sean

epots9's Avatar
Moderator
 
Join Date: May 2007
Location: Canada
Posts: 1,313
#2: Aug 31 '07

re: Creating checkboxes dynamically document.createElement


add this:

Expand|Select|Wrap|Line Numbers
  1. cb.name = "id";
  2.  
php post uses the name attribute, so since u didn't have one set it couldn't find what your looking for.

good luck
epots9's Avatar
Moderator
 
Join Date: May 2007
Location: Canada
Posts: 1,313
#3: Aug 31 '07

re: Creating checkboxes dynamically document.createElement


one thing i noticed is that all your checkboxes will have the same name/id, u'll need to set it up so they are different.
markrawlingson's Avatar
Moderator
 
Join Date: Aug 2007
Location: Bowmanville, Ontario
Posts: 329
#4: Aug 31 '07

re: Creating checkboxes dynamically document.createElement


Here you go, this should work just fine.. I set the name and created a method for counting the checkboxes as you create them. The number is then appended to the name of the checkboxes to you would references them by their number. In this way, each checkbox will indefinately have a unique name for which you can refer to in your php script. Note that the number appended to the name is kept track of using a hidden form field.

first checkbox created = 0, second = 1, etc (or you could replace the hidden form field's value with "1" - then first checkbox created = 1

..Well, you get the idea, i'm sure!

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.    <head>
  3.    <script language="Javascript">
  4.  
  5.      function append()
  6.      {
  7.         var i = parseInt(document.getElementById( "iCheckboxes" ).value);
  8.         var cb = document.createElement( "input" );
  9.         cb.type = "checkbox";
  10.         cb.id = "id"+i;
  11.         cb.name = "name"+i;
  12.         cb.value = "test";
  13.         cb.checked = true;
  14.         var text = document.createTextNode( "checkbox" );
  15.         document.getElementById( 'append' ).appendChild( text );
  16.         document.getElementById( 'append' ).appendChild( cb );
  17.         document.getElementById( "iCheckboxes" ).value = parseInt(document.getElementById( "iCheckboxes" ).value) + 1;
  18.      }
  19.  
  20.    </script>
  21.    </head>
  22. <body>
  23.    <p>click the button below</p>
  24.    <form action="http://localhost/test.php" name="form" id="form" method="post" enctype="multipart/form-data">
  25.    <div id="append" name="append">Append here</div>
  26.    <input type="hidden" value="0" name="iCheckboxes" id="iCheckboxes">
  27.    <input type="button" value="append" onclick="javascript:append()" />
  28.    <input type="submit" value="submit" />
  29.    </form>
  30. </body>
  31. </html>
  32.  
Member
 
Join Date: Jul 2007
Posts: 69
#5: Aug 31 '07

re: Creating checkboxes dynamically document.createElement


Thanks for your help guys. I didn't realise I missed setting the id. What I would like, however, is to have an array of checkboxes so they all have the same name/id
Expand|Select|Wrap|Line Numbers
  1.  cb.id = "id[]" cb.name = "id[]" 
but each checkbox has a different value. Then when the form is submitted I can get a list of the checkbox checked. It's a group of checkboxes really. When I do this I don't get anything through
Expand|Select|Wrap|Line Numbers
  1. $_POST
.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.    <head>
  3.    <script language="Javascript">
  4.  
  5.      function append()
  6.      {
  7.         var i = parseInt(document.getElementById( "iCheckboxes" ).value);
  8.         var cb = document.createElement( "input" );
  9.         cb.type = "checkbox";
  10.         cb.id = "id[]"
  11.         cb.name = "id[]"
  12.         cb.value = "test" + i;
  13.         cb.checked = true;
  14.         var text = document.createTextNode( "checkbox" );
  15.         document.getElementById( 'append' ).appendChild( text );
  16.         document.getElementById( 'append' ).appendChild( cb );
  17.         document.getElementById( "iCheckboxes" ).value = parseInt(document.getElementById( "iCheckboxes" ).value) + 1;
  18.      }
  19.  
  20.    </script>
  21.    </head>
  22. <body>
  23.    <p>click the button below</p>
  24.    <form action="http://localhost/test.php" name="form" id="form" method="post" enctype="multipart/form-data">
  25.    <div id="append" name="append">Append here</div>
  26.    <input type="hidden" value="0" name="iCheckboxes" id="iCheckboxes">
  27.    <input type="button" value="append" onclick="javascript:append()" />
  28.    <input type="submit" value="submit" />
  29.    </form>
  30. </body>
  31. </html>
  32.  
markrawlingson's Avatar
Moderator
 
Join Date: Aug 2007
Location: Bowmanville, Ontario
Posts: 329
#6: Sep 1 '07

re: Creating checkboxes dynamically document.createElement


Well,

You can't set a check box's value, for starters. A checked checkbox is returned as "on" to your server side script, an unchecked checkbox is not even returned at all and will be null. Check boxes are basically boolean type data. True or false. On or off. Checked or unchecked. 1 or 0. - etc

Why is $POST returning nothing? Er, well - I removed your enctype=multipart/form-data" and it does - including the value of "on" for all 5 checkboxes i created and checked. Though i'm not quite sure why that made a difference.. I think that enctype is used for the GET method to encrypt the data through the querystring.. not sure about that one though. Anyway, it works using POST if you remove that.

Mark
Member
 
Join Date: Jul 2007
Posts: 69
#7: Sep 1 '07

re: Creating checkboxes dynamically document.createElement


Hi Mark,

I don't know what I'm doing wrong. Here's my code again. This is all that I'm getting in $_POST if I create three checkboxes by clicking 'Append' and then 'Submit'.

Expand|Select|Wrap|Line Numbers
  1. array(0) { } array(1) { ["iCheckboxes"]=> string(1) "2" }
.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.    <head>
  3.    <script language="Javascript">
  4.  
  5.      function append()
  6.      {
  7.         var i = parseInt(document.getElementById( "iCheckboxes" ).value);
  8.         var cb = document.createElement( "input" );
  9.         cb.type = "checkbox";
  10.         cb.id = "id"+i;
  11.         cb.name = "name"+i;
  12.         cb.checked = true;
  13.         var text = document.createTextNode( "checkbox" );
  14.         document.getElementById( 'append' ).appendChild( text );
  15.         document.getElementById( 'append' ).appendChild( cb );
  16.         document.getElementById( "iCheckboxes" ).value = parseInt(document.getElementById( "iCheckboxes" ).value) + 1;
  17.      }
  18.  
  19.    </script>
  20.    </head>
  21. <body>
  22.    <p>click the button below</p>
  23.    <form action="http://localhost/test.php" name="form" id="form" method="post">
  24.    <div id="append" name="append">Append here</div>
  25.    <input type="hidden" value="0" name="iCheckboxes" id="iCheckboxes">
  26.    <input type="button" value="append" onclick="javascript:append()" />
  27.    <input type="submit" value="submit" />
  28.    </form>
  29. </body>
  30. </html>
  31.  
Maybe you could send me your code again.

Thanks,

Sean
markrawlingson's Avatar
Moderator
 
Join Date: Aug 2007
Location: Bowmanville, Ontario
Posts: 329
#8: Sep 1 '07

re: Creating checkboxes dynamically document.createElement


Quote:
This is all that I'm getting in $_POST if I create three checkboxes by
clicking 'Append' and then 'Submit'.
I'm assuming that by your statement above that after you created the 3 checkboxes you didn't check them. If a checkbox is not checked - it returns a value of null or "" or Empty - it's not returned to your code what so ever.

Otherwise the code looks pretty solid.

I will run it again later on, gotta go out for a bit here... but try creating 3 checkboxes, check them, and then submit the form and see what you get.

Sincerely,
Mark
Member
 
Join Date: Jul 2007
Posts: 69
#9: Sep 1 '07

re: Creating checkboxes dynamically document.createElement


Hi Mark,

I've been a right doofus!!!! You're right I wasn't checking the checkboxes. Only creating them. I should have realised by what you said before and I should have known any way that checkboxes are boolean.

Oh well thanks so much for your time. I'm very grateful.

Have a good evening.

Sean
markrawlingson's Avatar
Moderator
 
Join Date: Aug 2007
Location: Bowmanville, Ontario
Posts: 329
#10: Sep 1 '07

re: Creating checkboxes dynamically document.createElement


No problem!

Well actually a lot of people think that checkboxes are returned as "off" if they aren't checked, and "on" if they are - but it's simply just not returned what so ever if it's not checked!

It's kind of silly actually.. You wouldn't see boolean type data anywhere else that JUST has a value of TRUE if it's true and a value of NULL if it's false right?
Reply