mattgen88
mattgen88

Reputation: 291

post multiple multi-select boxes

I have a form that I'm using to create time ranges throughout a week. Right now it is select the day of week, start time and end time. I have javascript appending a new grouping of this same form and each element is named with an array.

<label>Day Of Week:</label> <select name="dow[]" multiple="multiple" size="5"><option value="sun">sun</option><option value="mon">mon</option><option value="tue">tue</option><option value="wed">wed</option><option value="thu">thu</option><option value="fri">fri</option><option value="sat">sat</option></select><br />
<label>Start Time</label> Hour: <select name="starthour[]"><option value="0">12 AM</option><option value="1">1 AM</option><option value="2">2 AM</option><option value="3">3 AM</option><option value="4">4 AM</option><option value="5">5 AM</option><option value="6">6 AM</option><option value="7">7 AM</option><option value="8">8 AM</option><option value="9">9 AM</option><option value="10">10 AM</option><option value="11">11 AM</option><option value="12">12 PM</option><option value="13">1 PM</option><option value="14">2 PM</option><option value="15">3 PM</option><option value="16">4 PM</option><option value="17">5 PM</option><option value="18">6 PM</option><option value="19">7 PM</option><option value="20">8 PM</option><option value="21">9 PM</option><option value="22">10 PM</option><option value="23">11 PM</option></select>Minute: <select name="startmin[]"><option value="0">0</option><option value="15">15</option><option value="30">30</option><option value="45">45</option></select> <br /> 
<label>End Time</label> Hour: <select name="endhour[]"><option value="0">12 AM</option><option value="1">1 AM</option><option value="2">2 AM</option><option value="3">3 AM</option><option value="4">4 AM</option><option value="5">5 AM</option><option value="6">6 AM</option><option value="7">7 AM</option><option value="8">8 AM</option><option value="9">9 AM</option><option value="10">10 AM</option><option value="11">11 AM</option><option value="12">12 PM</option><option value="13">1 PM</option><option value="14">2 PM</option><option value="15">3 PM</option><option value="16">4 PM</option><option value="17">5 PM</option><option value="18">6 PM</option><option value="19">7 PM</option><option value="20">8 PM</option><option value="21">9 PM</option><option value="22">10 PM</option><option value="23">11 PM</option></select>Minute: <select name="endmin[]"><option value="0">0</option><option value="15">15</option><option value="30">30</option><option value="45">45</option></select> <br />

This form is generated with javascript so that multiple rules can be submitted. Everything works fine aside from the multiselect boxes.

If a user selected in rule one mon and wed, and in rule 2 selected tue, they get combined into the same array on the form. So a dump of $_POST will show:

Array ( [dow] => Array ( [0] => mon [1] => wed [2] => tue ) [starthour] => Array ( [0] => 0 [1] => 15 ) [startmin] => Array ( [0] => 0 [1] => 0 ) [endhour] => Array ( [0] => 5 [1] => 17 ) [endmin] => Array ( [0] => 0 [1] => 0 ) [do] => add [act] => scheduler [mod] => availability [avtype] => perm )

I have no way of determining which rule the data in dow is from. I don't believe I'm doing anything outwardly wrong. Currently the only fix I have is to only allow one day selected. However it is common that people have the same times over multiple days. It would be nice to include this functionality if at all possible.

Upvotes: 0

Views: 643

Answers (2)

yajakass
yajakass

Reputation: 279

I would do it this way.. for each rule i will get a timestamp and select element in generated form will look like this..

    select name="dow[timestamp][]" ...     

so all your rule will have unique array now.

you can get timestamp with following code.

function microtime (get_as_float) 
{
    var now = new Date().getTime();
    return now;
}

Upvotes: 0

danbgray
danbgray

Reputation: 582

In your javascript, uniquely name each form with an id - this will work if there are multiple forms, but might not be the best solution.

maybe the best thing to do is in your javascript form generation, add var i; name=i+"dow[]" so you can identify the individual form elements. I don't believe you can distinguish between form elements on the same form with the same name, at least not on post. You could loop through those elements with javascript pre-post and assign unique identifiers on post.

Upvotes: 1

Related Questions