1 Vote Vote

Multiple Select in PHP - How to edit data?

Posted by topdog 339 days ago Questions| option multiple select All

it is easy to create a form that will allow a user to select multiple values and then get the data that was entered. What I want to know is how to recreate that form based on the information in the database? ie Edit the data.

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test[]" multiple="multiple">
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
    <option value="four">four</option>
    <option value="five">five</option>
</select>
<input type="submit" value="Send" />
</form>

<?php
    $test=$_POST['test'];
    if ($test){
     foreach ($test as $t){echo 'You selected ',$t,'<br />';}
    }
?>

Say values two and foru were seleted in the original data. How would I recreate the form to edit the data like the code below:

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test[]" multiple="multiple">
    <option value="one">one</option>
    <option selected value="two">two</option>
    <option value="three">three</option>
    <option selected value="four">four</option>
    <option value="five">five</option>
</select>
<input type="submit" value="Send" />
</form>

Initially I want the code for add and edit to work. Eventually I want to be able to enter various multiple select options in a form and to edit them.

enter image description here

Originally asked by: andrebruton on Stack Overflow

Discuss Bury


Who Voted for this Question