Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 548
  • Last Modified:

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

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
Das246
Asked:
Das246
  • 3
  • 2
1 Solution
 
JakobACommented:
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
 
ZontarCommented:
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
 
JakobACommented:
>> 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Das246Author Commented:
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
 
ZontarCommented:
> 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
 
JakobACommented:
<eating crow>  apparently I am gettting too old in this business. sorry
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now