... well, not exactly. I used to have code like this:

<input type="checkbox" name="defender[]" id="def1" value="Cafu" />Cafu (AC Milan)<br />

So I could check various boxes, make an array called 'defender' and pass it to another page. Cool.

Except, I put a javascript validation on the form and it wouldn't work with the [] so I had to remove them:

<input type="checkbox" name="defender" id="def1" value="Cafu" />Cafu (AC Milan)<br />

That works fine for the validation, but now when I try to pass the values to the next page, only the last value in the 'array' appears.

How can I turn the values into an array without using [] in the name attribute of the checkbox?

If anyone can help I'll provide full code for you to look at.

    As is often the case, that's led me on to a new problem...

    This is my javascript code:

    function validate()
    {
    var totalgol = 0;
        var maxgol = document.forms[0].elements['goalkeeper[]'].length;
        for (var idx = 0; idx < maxgol; idx++)
            {
            if (eval("document.forms[0].elements['goalkeeper[" + idx + "]'].checked") == true)
                {
                totalgol += 1;
                }
            }
        if (totalgol != 1)
            {
            alert ("Please choose a goalkeeper");
            return false;
            }
    return true;
    }
    

    And this is my form:

    <form action="new_pick2.php" name="pickateam" method="post" onsubmit="return validate();">
    <input type="checkbox" name="goalkeeper[]" id="gol1" onClick="countGol(this)" value="Dida" />Dida (AC Milan)<br />
    <input type="checkbox" name="goalkeeper[]" id="gol2" onClick="countGol(this)" value="Julio Cesar" />Julio Cesar (Flamengo)
    </form>
    

    But when I sumbit the form I get a Javascript error:

    Error: document.forms[0].elements['goalkeeper[0]'] has no properties

    And the value is passed into the next page as the word 'Array'.

    Sorry, I know this is more Javascript than PHP, but it's combining the two that's killing me.

      Don't use javascript to validate things.

      Fact: Over 10% of web users have client-side scripting switched off.

        Let's say I'm doing this for myself, just to see if I can.

          So, how do you recommend we do validation then??

          Thanks

          BIOSTALL

            Write a Reply...