Yeah this is a javascript question, but I'm asking it here since I'm sure someone else has run into this.

I have a select box, with multiple options, which can be multiselected. I wrote a little script to select and de-select all, but then I remember I had to name the form element as "name[]" so php will process it as an array.

Javascript really doens't like me referencing a form element that ends in brackets. I've tried escaping them, but that won't work either.

Anyone found a solution for this?

    Don't reference the name; reference an ID:

    <select name="something[]" id="mySelectBox">

    and the JS:

    document.getElementById('mySelectBox').selectedIndex

    etc. etc.

      For:
      <input type="text" name="Settings[Name]">
      Use:
      document.forms.FormNameHere.elements["Settings[Name]"].value

      It's documented in the php manual under the user comments for arrays. And there are a few (dozen) threads asking the same question.

      Using an id is probably better but not always practical.

        2 years later

        I've got the same problem...thank you search function!

        So with the above code, the select box works, but instead of the actual values, I get this for results:

        "array"

        My POST statement looks like this:

        $variable = $_POST['listRight'][];

          Got this working fine by removing the [] from the POST statement

          $variable = $_POST['listRight'];

          Then I just used a foreach statement to insert each value in the array into my db table.

            Write a Reply...