473,378 Members | 1,152 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Creating checkboxes dynamically document.createElement

69
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
Aug 31 '07 #1
9 48386
epots9
1,351 Expert 1GB
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
Aug 31 '07 #2
epots9
1,351 Expert 1GB
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.
Aug 31 '07 #3
markrawlingson
346 Expert 100+
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.  
Aug 31 '07 #4
Sebarry
69
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.  
Aug 31 '07 #5
markrawlingson
346 Expert 100+
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
Aug 31 '07 #6
Sebarry
69
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
Sep 1 '07 #7
markrawlingson
346 Expert 100+
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
Sep 1 '07 #8
Sebarry
69
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
Sep 1 '07 #9
markrawlingson
346 Expert 100+
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?
Sep 1 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: oeyvind toft | last post by:
How do I create a checkbox element ? Or any other input type ? document.createElement('checkbox') doesnt seem to work. Oeyvind -- http://home.online.no/~oeyvtoft/ToftWeb/
2
by: Dave | last post by:
I have a page which uses JavaScript to create form elements using document.createElement('input'), etc.. Both Firefox and IE have no problem accomplishing this and when the form is submitted all...
5
by: SalamElias | last post by:
I am creating several chkBoxes dynamically and assigning an event handler in the Page_load as foillows ***************************** Dim chkCatOption As CheckBox = New CheckBox chkCatOption.Text...
10
by: webEater | last post by:
Hello, I try the following in Firefox and other modern browsers: window.addEventListener('load', function() { document.title = CSS.getClass('fontSize'); var div = document.createElement('div');...
4
by: Narven | last post by:
Hi, I've been creating a script that dynamic loads js files. but after creating that script, (and i use document.createElement('script');) in that function.. i've realise that the code that...
4
by: tswaters | last post by:
Alright, so generally I'm using document.createElement("IMG").... but, I've noticed something just recently that made me switch to "new Image()" What I'm doing is creating a photo gallery of...
1
by: emzipoo4u | last post by:
Hi, I am creating an XML document with ASP (new to XML). I would like to know how do i add details to a tag e.g. i want to generate <Order xmlns="urn:www.badjda.org/schema/test_order_v3.00.xml">...
4
by: ShutterMan | last post by:
I have a function that creates input buttons dynamically... function $NEW_BUTTON(id, content, parentId, cssclass, onclick) { var ctrl = document.createElement("input"); ctrl.id = id;...
23
by: vunet | last post by:
It is recommended by some sources I found to create IFrames in IE using document.createElement('<iframe src="#">') instead of document.createElement('iframe'). Why and what browser versions to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.