Consider "name" over "id" multiple select?!?!

I’m using a multiple select box and want to be able to get the name=“” instead of id=“” into my $_POST[‘’]; is that possible…?

The reason for this is taht I use the id=“” for an javascript but want to be able to insert into mysql using array…

Here is how it looks…

<select multiple id="choose" name="choose[]">

As you can see I need the brackets “” in the name for the array but can’t use them in my id=“” due to an javascript function…

Thanks in advance :slight_smile:

Erm, the id attribute is never passed to scripts, only the name attribute…

When using forms, the name attribute is used to pass the data, as SJH has said. The id attribute is for presentation (using CSS) or client-side scripting (using javascript and DOM).

Edit:

You can use the name attribute with javascript, as well, but it’s usually more efficient to use the id.

php strips the brackets off when it populates _GET/_POST, because it parses it into an array structure for you.

Why do you think you need the brackets as part of the name(within php)?

Sorry… It seems like my problem is completely different!!! I have a script in which I choose members from 1 selectbox to another… When I pass the users to the second selectbox thay are no chosen/marked and when I then submits the form… ofcourse there is no records… Is there a way that I can make every record in a select box count even though they are not marked!!!

Hop this is to understand… :expressionless:

Store separate lists as hidden input fields.


<input type="hidden" name="chosen[]" value="foo">
<input type="hidden" name="chosen[]" value="bar">

Not quite sure what you mean?!?

Presumably, you’re using javascript to transfer values between the two select menus?

Yes…

Ok. So I’m suggesting that when you add an option to the select element, you also create a hidden input element which matches the value you just added.

You could also just loop through all the options and set the selected attribute to true using an event handler for the forms submit event.

But how do I insert multiple records into an hidden input element?

You don’t. You create multiple hidden input elements. see createElement() to make an input element, and appendChild() to append it to the form element.