Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

subscripted checkbox name processing 1

Status
Not open for further replies.

awingnut

Programmer
Joined
Feb 24, 2003
Messages
759
Location
US
I have a set of checkboxes in a form that have subscripted names so that I can pass them to a PHP script as an array. However, I need to do some client side checking first (at least one must be checked). I can't seem to get the right syntax in my javascript so that I can check the values.
Code:
<input name="carrier[0]" type="checkbox" value="blah">blah
<input name="carrier[1]" type="checkbox" value="blah2">blah2
<input name="carrier[2]" type="checkbox" value="blah3">blah3
<input name="carrier[3]" type="checkbox" value="blah4">blah4
When I refer to 'theForm.carrier.length' I get an error saying it has no properties. How do I reference the object so that I can loop through to make sure at least one is checked? TIA.
 
i am confused, but i am not condescending. make me understand why people do this for php? i've used php before and have never done this type of "subscripting". can't you just name them all "carrier" without the square brackets, then loop through the array?

there are no form fields named "carrier" in the code you've given. you'll have to get ALL form elements, then loop through them all and test for a name beginning with "carrier", perhaps filling another array or just ignoring those form fields that do not have a name starting with "carrier".



*cLFlaVA
----------------------------
spinning-dollar-sign.gif
headbang.gif
spinning-dollar-sign.gif

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Thanks for the reply (and so quick).

If it is not subscripted then it is not an array in $_POST. I don't understand the reason (PHP implementation) but the $_POST['carrier'] will then contain only 1 value and that is the value of the last checkbox that was checked. Any other checkbox values will be lost.

I don't have a problem looping through all the objects but I'm not sure how to do that. Does 'theForm.length' give me the number of objects and 'theForm[n].name' give me each one?
 
here is one way (simplified):

Code:
function myFunc() {
    var f = document.forms['myFormName'];
    var e = f.elements;

    for ( var i = 0; i < e.length; i++ ) {
        if ( e[i].name.indexOf("carrier") > -1 ) {
            alert(e[i].name + " has a value of " + e[i].value);
        }
    }
}



*cLFlaVA
----------------------------
spinning-dollar-sign.gif
headbang.gif
spinning-dollar-sign.gif

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
That's what I needed. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top