asmith Posted November 29, 2007 Share Posted November 29, 2007 <select name="test[]" multiple="multiple"> <option value="1"> 55 </option> <option value="2"> 66 </option> <option value="3"> 77</option> </select> the code above without "[]" , no matter how many choices been selected , overwrite to last one, putting [] , makes all selected a array like : $test = $_POST[test]; $test[0] $test[1] $test[2] // the values , why this code won't work properly ? (it has to remain the selected item for each pge reload, but it makes all confusing.mixed <?php echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'" >'; $options = array ("1" => "55","2" => "66","3" => "77"); echo '<select name="test[]" multiple="multiple">'; $test = $_POST[test]; $i = count($test); $a = 0; while(list($aa,$bb) = each($options)) { if ($a < $i){ if ((isset($_POST['test1'])) AND ($test[$a] == $aa)) {$sel = 'selected="selected"';$a++;} } echo '<option value='.$aa.' '.$sel.'>'.$bb.'</option>'; } echo '<input type="submit" name="test1" value="test" /></select></form>'; ?> (BTW any more simple idea ? less coding ? ) Quote Link to comment https://forums.phpfreaks.com/topic/79372-solved-help-me-with-this-code-please/ Share on other sites More sharing options...
Barand Posted November 29, 2007 Share Posted November 29, 2007 try <?php echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'" >'; $options = array ("1" => "55","2" => "66","3" => "77"); echo '<select name="test[]" multiple="multiple">'; while(list($aa,$bb) = each($options)) { $sel = in_array($aa,$_POST['test']) ? 'selected="selected"' : ''; echo "<option value='$aa' $sel>$bb</option>"; } echo '<input type="submit" name="test1" value="test" /></select></form>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/79372-solved-help-me-with-this-code-please/#findComment-401891 Share on other sites More sharing options...
asmith Posted November 29, 2007 Author Share Posted November 29, 2007 it works !!! ahhhh ! thanks a lot !! Quote Link to comment https://forums.phpfreaks.com/topic/79372-solved-help-me-with-this-code-please/#findComment-401900 Share on other sites More sharing options...
Barand Posted November 29, 2007 Share Posted November 29, 2007 BTW. I just noticed the last line should be echo '</select><input type="submit" name="test1" value="test" /></form>'; closing the "select" before the button Quote Link to comment https://forums.phpfreaks.com/topic/79372-solved-help-me-with-this-code-please/#findComment-401901 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.