Solved

Can Dynamic Form Elements as an array be referenced in both PHP and Javascript?

Posted on 2003-11-15
6
545 Views
Last Modified: 2013-11-19
Hope someone can help.

I have a page that loads dynamically from a database and also has checkboxes. Before the program starts it doesn't know how many elements there will be so I had to set up the elements as an array.

I need to reference the submitted array of elements both in PHP and in Javascript.

When I call the elements by the name 'checkbox[]' I can see the array in PHP coding but I can't reference the array from javascript. Eg...

document.formname.checkbox[0].checked = true;

If I name the elements 'checkbox' then my javascript code works but PHP no longer sees the form elements as an array.

Is there an easy way for Javascript to recognise the 'checkbox[]' name?
0
Comment
Question by:Das246
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 15

Accepted Solution

by:
JakobA earned 30 total points
ID: 9757560
The reason you cannot see them i javascript is that the name 'checkbox[]'  is not a proper javascript name. you need to do some special adressing to get at them with javascript:

function getCBarray ( ) {
     var horribleName = "checkbox[]";
     var formObj = document.forms["formNavn"];   // use the name of your form.
     var cblist = new Array();
     for (var i=0; i<formObj.elements.length; i++) {
          if ( formObj.elements[i].name = horribleName )  {
              cblist[cblist.length] = formObj.elements[i];
          }
     }
     return cblist;
} //end getCBarray

var arrayOfCheckBoxes = getCBarray ( );   // return an array with those checkboxes;

alert( "The first of the checkboxes is " +arrayOfCheckBoxes[0].checked );

regards JakobA
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9757753
given horridName = "my_checkbox[]"

document.formName.elements[horridName] is itself an array and can be accessed using

document.formName.elements[horridName][0], document.formName.elements[horridName][1], etc.,

without the need to create a new array just for this purpose. Example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title></title>

<style type="text/css">
</style>

<script type="text/javascript">
function display()
{
  var form = document.forms[0];
  var horridName = "bleh[]";
  var msg = "";

  for(i = 0; i < form.elements[horridName].length; i++)
    if(form.elements[horridName][i].checked)
      msg += form.elements[horridName][i].value + "<br>";

  document.getElementById("thing").innerHTML = msg;
}
</script>

</head>
<body>
<form>
<p>
1. <input type="checkbox" name="bleh[]" value="meh1"><br>
2. <input type="checkbox" name="bleh[]" value="meh2"><br>
3. <input type="checkbox" name="bleh[]" value="meh3"><br>
4. <input type="checkbox" name="bleh[]" value="meh4"><br>
5. <input type="checkbox" name="bleh[]" value="meh5">
</p>
<p><input type="button" value="Do it." onclick="display();"></p>
</form>
<p id="thing"></p>
</body>
</html>

I really like PHP but this is really stupid and should NEVER have been implemented in this fashion. It is true that name="my_checkbox[]" is perfectly valid HTML as names may contain just about any characters (e.g. name="00%#@~ER**(" is also legal) since name values are of type CDATA -- however, the decision to allow non-alphanumerics for form control names in PHP was really incredibly thoughtless.
0
 
LVL 15

Expert Comment

by:JakobA
ID: 9757769
>> Das246
Thanks :)

>> Zontar
Yes,  On encountering multiple identical field-names i a form the IE browser build an array.
But it is the only browser with that behaviour so you should not rely on it.

regards JakobA
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:Das246
ID: 9757784
Hi guys,

Thanks for both your help. I had been struggling with this for a while today and hadn't considered form.elements["checkbox[]"][0] etc. I tried Zontar's code and it worked fine in both IE and Netscape.

cheers

Das246
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9757897
> Yes,  On encountering multiple identical field-names i a form the IE browser build an array.
> But it is the only browser with that behaviour so you should not rely on it.

Oh, really? That's very interesting to hear you say so, since I tested that in Mozilla and Opera and it slipped my mind to try it in MSIE at all. <G>
0
 
LVL 15

Expert Comment

by:JakobA
ID: 9758355
<eating crow>  apparently I am gettting too old in this business. sorry
0

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question