I have a form that I am trying to post to a table, everything works fine except for the one multiple select option I have. When it posts to the table, only the last selected option shows. How do I get it to either a. post to separate fields in the table or b. post to the same field separated by a delimiter??

Can you please explain it.

Can you please explain it.

This is the code for the form input:

<select name="grades" size="5" multiple id="grades">
                          <option  value="Pre K">Pre K</option>
                          <option  value="Kindergarten">Kindergarten</option>
                          <option  value="1st">1st</option>
                          <option  value="2nd">2nd</option>
                          <option  value="3th">3rd</option>
                          <option  value="4th">4th</option>
                          <option  value="5th">5th</option>
                          <option  value="6th">6th</option>
                          <option  value="7th">7th</option>
                          <option  value="8th">8th</option>
                          <option  value="9th">9th</option>
                          <option  value="10th">10th</option>
                          <option  value="11th">11th</option>
                          <option  value="12th">12th</option>
                        </select>

Since there is an option to select multiple answers, I would like for all of the answers to be recorded in the database however, the only answer it posts it the last of the group. for example say 6th, 7th, and 8th were all selected. When the form is submitted, only "8th" gets posted to the correct field. Does that make sense?

This is the code for the form input:

<select name="grades" size="5" multiple id="grades">
                          <option  value="Pre K">Pre K</option>
                          <option  value="Kindergarten">Kindergarten</option>
                          <option  value="1st">1st</option>
                          <option  value="2nd">2nd</option>
                          <option  value="3th">3rd</option>
                          <option  value="4th">4th</option>
                          <option  value="5th">5th</option>
                          <option  value="6th">6th</option>
                          <option  value="7th">7th</option>
                          <option  value="8th">8th</option>
                          <option  value="9th">9th</option>
                          <option  value="10th">10th</option>
                          <option  value="11th">11th</option>
                          <option  value="12th">12th</option>
                        </select>

Since there is an option to select multiple answers, I would like for all of the answers to be recorded in the database however, the only answer it posts it the last of the group. for example say 6th, 7th, and 8th were all selected. When the form is submitted, only "8th" gets posted to the correct field. Does that make sense?

Use array. Ie.,

<select name="grades[]" size="5" multiple id="grades">

Then, as usual, use foreach($_POST['grades'] as $value) to get each of the selected value.

for each is the best option in this case

I tried, now instead of giving me back the last grade selected, it just prints "array". I tried to implode but it still posts the same thing!

Hi,

Use the following code.

<?
 $selectedGrades=$_POST['grades']; 
foreach ($selectedGrades as $item) 
echo $item."<br>\n";
?>

<form name=frm action="" method="post">
<select name="grades[]" size="5" multiple id="grades[]">
                          <option  value="Pre K">Pre K</option>
                          <option  value="Kindergarten">Kindergarten</option>
                          <option  value="1st">1st</option>
                          <option  value="2nd">2nd</option>
                          <option  value="3th">3rd</option>
                          <option  value="4th">4th</option>
                          <option  value="5th">5th</option>
                          <option  value="6th">6th</option>
                          <option  value="7th">7th</option>
                          <option  value="8th">8th</option>
                          <option  value="9th">9th</option>
                          <option  value="10th">10th</option>
                          <option  value="11th">11th</option>
                          <option  value="12th">12th</option>
                        </select>

<input type=submit value=submit>
</form>

use foreach($_POST as $value) to get each of the selected value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.