Jump to content

[SOLVED] help me with this code please


asmith

Recommended Posts

<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 ? )

Link to comment
https://forums.phpfreaks.com/topic/79372-solved-help-me-with-this-code-please/
Share on other sites

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>';

?>   

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.