Jump to content

remaining value for select option


asmith

Recommended Posts

i've used $_SERVER

[php_SELF] , so my form and script are just one file .
when a user click the submit and have some errors the sript reload again ,
for a reloaded script i've put :(for exaple)

<input type="text" name="username" value=$_POST[username] />

 

so when it reload the last information the uer has typed won't be delete.it stays.

i want to do the same with my "select menu " , i don't know how , the select menu reset when script reload :(example)

 

gender : 
<select>
<option value="">select one</option>
<option value="m">male</option>
<option value="f">female</option>
</select>

 

Link to comment
https://forums.phpfreaks.com/topic/78775-remaining-value-for-select-option/
Share on other sites

I think you may be looking for this:

 

<?php
echo "gender : 
<select>
<option value=\"\">select</option>
";
if ( $_POST['gender'] == 'm' ) {
    echo "<option value=\"m\" selected>male</option>
";
} else {
    echo "<option value=\"m\">male</option>
";
if ( $_POST['gender'] == 'f' ) {
    echo "<option value=\"f\" selected>female</option>
";
} else {
    echo "<option value=\"f\">female</option>
";
echo"</select>";
?>

 

PhREEEk

Edit: This is a shorter code to PHP_PhREEEk's approach.

<?
${"select_".$_POST[gender]} = "selected";
print '<select>';
print '<option value="null" '.$select_null.'>select</option>';
print '<option value="m" '.$select_m.'>male</option>';
print '<option value="f" '.$select_f.'>female</option>';
print '</select>';

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.