asmith Posted November 28, 2007 Share Posted November 28, 2007 <form action="$_SERVER['PHP_SELF'] method="post"> <input type="text" name="hello" value="$_POST['hello'] /> <input type="submit" value="test" /> </form> the above code , by pressing test (submit) button reload the page, and if user had typed anything to the field, it remains. i can't do the same thing with select option : (and more important to me, multiple select options) <form action="$_SERVER['PHP_SELF'] method="post"> <select name="test" > <option value="1"> 1 </option> <option value="1"> 2 </option> <option value="1"> 3 </option> </select> <input type="submit" value="test" /> </form> the best thing i could do was : <form action="$_SERVER['PHP_SELF'] method="post"> <select name="test" > <option value="$_POST[test]"> $_POST[test] <option> <option value="1"> 1 </option> <option value="1"> 2 </option> <option value="1"> 3 </option> </select> <input type="submit" value="test" /> </form> this code , when the page reload , saves the users choice, but if the user click on the menu , he will saw his selected option 2 times! the problem gets bigger, when i have about 20 options , that they can be choose multiple. anyone help ? Quote Link to comment Share on other sites More sharing options...
Wes1890 Posted November 28, 2007 Share Posted November 28, 2007 Put each select option into an array: <?php $options = array ( "1" => "Im 1", "2" => "Im 2", "3" => "Im 3" ); ?> Then, when displaying the select, do this: <?php echo "<select name='test'>"; // start the dropdown menu while ( list($id,$name) = each($options) ) // for each option in the array { $sel = ""; // keep blank by default if (isset($_POST['select']) AND $_POST['select'] == $id) // if the post-select option is available, check if it's ours { $sel = "selected"; // makes it selected } echo "<option id={$id} {$sel}>{$name}</option>"; // show the option } echo "</select>"; //all done ?> Quote Link to comment Share on other sites More sharing options...
asmith Posted November 28, 2007 Author Share Posted November 28, 2007 the id you mentioned in the code , you meant the value of an option ? Quote Link to comment Share on other sites More sharing options...
Wes1890 Posted November 28, 2007 Share Posted November 28, 2007 ^ Yaa my bad i was trying to type it quick Quote Link to comment Share on other sites More sharing options...
asmith Posted November 28, 2007 Author Share Posted November 28, 2007 ooh yea, it will work i guess, havn't tried yet , but understood what you did there ! (90%) thank you so much ! that 1 => im 1, what it has todo with the code exactlly ? (10% ^^ ) Quote Link to comment Share on other sites More sharing options...
Wes1890 Posted November 28, 2007 Share Posted November 28, 2007 ^ It sets the value and title (whats shown) for each select's option, otherwise it wont know how many options to create. You can set it to whatever you want, like.. "Post1" => "asmith", "Post2" => "Wes1890", "Smith" => "asmith", "Wes" => "Wes1890", whatever you want Quote Link to comment Share on other sites More sharing options...
asmith Posted November 29, 2007 Author Share Posted November 29, 2007 your code do not work ,!! someone help ! Quote Link to comment 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.