Solved

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

Posted on 2003-11-15
6
544 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
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.
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 how to count occurrences of each item in an array.

734 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