Solved

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

Posted on 2003-11-15
6
533 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
  • 3
  • 2
6 Comments
 
LVL 15

Accepted Solution

by:
JakobA earned 30 total points
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:Das246
Comment Utility
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
Comment Utility
> 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
Comment Utility
<eating crow>  apparently I am gettting too old in this business. sorry
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now