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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.