Solved

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

Posted on 2003-11-15
6
543 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Link Anchor Not working 9 67
javascript and jquery question 4 45
Validating number not work with decimal 4 45
Javascript question 8 43
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

732 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