views:

70

answers:

5

This is a two part question. Someone answered a similar question the other day (which also contained info about this type of array in PHP), but I cannot find it.

1.) First off, what is the correct terminology for an array created on the end of the name element of an input tag in a form?

<form>

    <input name="p_id[]" value="0"/>
    <input name="p_id[]" value="1"/>
    <input name="p_id[]" value="2"/>

</form>

2.) How do I get the information from that array with JavaScript? Specifically, I am right now just wanting to count the elements of the array. Here is what I did but it isn't working.

function form_check(){
    for(var i = 0; i < count(document.form.p_id[]); i++){  //Error on this line

        if (document.form.p_name[i].value == ''){
            window.alert('Name Message');
            document.form.p_name[i].focus();
            break;
        }

        else{
            if (document.form.p_price[i].value == ''){
                window.alert('Price Message');
                document.form.p_price[i].focus();
                break;
            }

            else{
                update_confirmation();
            }
        }
    }
}
+1  A: 

document.form.p_id.length ... not count().

You really should give your form an id

<form id="myform">

Then refer to it using:

var theForm = document.getElementById("myform");

Then refer to the elements like:

for(var i = 0; i < theForm.p_id.length; i++){
Andir
Thanks, but this does not work
typoknig
A: 
for(var i = 0; i < count(document.form.p_id[]); i++) {

For this line use like this,

var cntval = document.form.p_id.length;
for(var i = 0; i < cntval; i++) {
Karthik
Thanks, but this does not work
typoknig
It is surely work. Otherwise you did anything wrong in somewhere...
Karthik
It doesn't work. JavaScript doesn't have a `count` method built it, and you can't use `[` or `]` in a property name that you access via dot notation. As for your second example, the fields are named `p_id[]` not `p_id`.
David Dorward
+1  A: 

1.) First off, what is the correct terminology for an array created on the end of the name element of an input tag in a form?

"Confusing PHPism".

As far as JavaScript is concerned a bunch of form controls with the same name are just a bunch of form controls with the same name, and form controls with names that include square brackets are just form controls with names that include square brackets.

The PHP naming convention for form controls with the same name is sometimes useful (when you have a number of groups of controls so you can so things like:

<input name="name[1]">
<input name="email[1]">
<input name="sex[1]" type="radio" value="m">
<input name="sex[1]" type="radio" value="f">

<input name="name[2]">
<input name="email[2]">
<input name="sex[2]" type="radio" value="m">
<input name="sex[2]" type="radio" value="f">

) … but is mostly just confusing.

2.) How do I get the information from that array with JavaScript?

Get the "name of form control" property of the elements property of the form. Since the name of the form controls includes square brackets, you can't use dot notation and have to use square bracket notation. Since you have multiple elements with that name, it will be a collection rather then a single control, so you can loop over it with a standard for loop that makes use of its length property.

var myForm = document.forms.id_of_form;
var myControls = myForm.elements['p_id[]'];
for (var i = 0; i < myControls.length; i++) {
    var aControl = myControls[i];
}
David Dorward
Thanks, this is exactly what I was looking for. I modified it a little though... `for(i = 0; i < document.form.elements['p_id[]'].length; i++)`
typoknig
Using `i` as a global isn't a good idea (it is just asking for scoping issues to bite you, either now or in the future because you have got into bad habits). Using `document.form` is bad practice because (a) the `document.forms` collection will make it clearer what you are doing and `form` isn't a very detailed name (and `document.form` could be confused with `document.forms` which makes maintenance more confusing. Scrunching things up on to a single line is fine though, I was just more explicit so you could see what is going on.
David Dorward
+1  A: 

Try this something like this:

var p_ids = document.forms[0].elements["p_id[]"];
alert(p_ids.length);
for (var i = 0, len = p_ids.length; i < len; i++) {
  alert(p_ids[i].value);
}
A: 
jmoreno
Wow. That's just ugly. Using `new Array()` instead of `[]`, keeping a length tracker instead of using `arr.length` or `arr.push()`, initializing `i` outside the for loop, using `getAttribute` instead of just going for the name property, working globally instead of within a form, and all the get (more or less) the same results of `document.forms.element.elements_name`. (Oh and `getElementsByName` could have been used too).
David Dorward