0

Hi everyone,

I have a drop menu as below and when submitted it sends either no value, male or female to the database.

<select id="gender" name="gender">
  <option selected="selected" value=""/> </option>
  <option value="Male">Male</option>
   <option value="Female">Female</option>
</select>

Now when i go back to the page i want it to show the value that was previously selected/stored in database.

Basically the php code i have at the moment is to get the values and it sends to database. I already have a while loop so all i need to do is $row[gender] to get get value but how i implement it into the drop menu html code i don't know.

<?php $gender = $_POST['gender']; ?>

Can someone please help on how i show the previously selected value that is stored in the database.

Thanks for any help.

Edited by genieuk: n/a

3
Contributors
3
Replies
9
Views
7 Years
Discussion Span
Last Post by genieuk
0

OK, I assume you want this value to persist beyond a simple round trip from form page -> handler page -> form page. I which case, I suggest using a cookie or a session variable.

THIS IS A QUICK/DIRTY FIX - COULD BE OPTIMISED
===================================

In your handler page:

$_SESSION['gender'] = $_POST['gender'];

In your form page:

if(isset($_SESSION['gender']) || $_SESSION['gender']!=''){
   $form_options = "\n\t<option selected=\"selected\" value=\"\"/> </option>";
   if($_SESSION['gender'] == 'Female'){
       $form_options .="\n\t<option value=\"Male\">Male</option>\n\t<option value=\"Female\" selected=\"selected\">Female</option>";
   }else{
       $form_options .="\n\t<option value=\"Male\" selected=\"selected\">Male</option>\n\t<option value=\"Female\">Female</option>";
   }
}else{
   $form_options = "\n\t<option selected=\"selected\" value=\"\"/> </option>\n\t<option value=\"Male\">Male</option>\n\t<option value=\"Female\">Female</option>';
}

....

<select id="gender" name="gender">
    <?php echo $form_options;?>
</select>

Like I said, this code ain't pretty and I'm a little embarrassed to put it up, but it should work.

0

how dose this help i have comments inside of the code that explain
let me know if it helped

<?php
// this is where we define if the user is loged in
// then there is a cookies and we need to select
// the one with the users id and we need to define it to a var
$uid = $_COOKIE['uid'];

// first off we need to connect to the database
// then we need to select a database now lets start
// Enter your database connect username and password in the area username and password
@mysql_connect('localhost', 'username', 'password');
// replace database name with you database name that you want to select
@mysql_select_db('databasename');

// now we need to define the $_POST[]; Var
// in the $gender var we are going to use some shorthand
// if statement were going to ask if it is not empty
// use $_POST['gender'] is it is not empty else
// then were going to say '' for blank
// then ? = then : = else
$gender = !empty($_POST['gender']) ? $_POST['gender'] : '';

// now we need to write some statement to know if the
// now were going to ask is it is empty again if no
// the if statement will be use if it is empty then
// it will do nothing or use the elseif or the else
// statements
if ($gender) {
	// if you changed the gender and the $gender is not empty
	// then we need to update the gender of the user as below
	mysql_query("UPDATE users SET gender = '$gender' WHERE id = '$uid'");
}

// now where going to get the gender out of the users table
// now we need to select the gender From the users where id = users id
$query = mysql_query("SELECT gender FROM users WHERE id = '$uid'");
// now we need to fetch the data from the query var in to an array
$getGender = mysql_fetch_array($query);
// now we write some more statements for what geneder to be selected
// if gender is = 0 then the male = selected
if ($getGender['gender'] = 0) {
	$male = 'selected="selected"';
}
// if else the gender = 1 then the female = selected
elseif ($getGender['gender'] = 1) {
	$female = 'selected="selected"';
}
// if gender dose not = none of the above then it = Select Gender
else {
	$empty = 'selected="selected"';
}
?>

<html>

<body>
<form method="post">
	<select name="gender">
		<option value="0" <?php print($empty); ?>>Select Gender</option>
		<option value="1" <?php print($male); ?>>Male</option>
		<option value="2" <?php print($female); ?>>Female</option>
	</select>
	<input type="submit" value="Save"/>
</form>
</body>
</html>

i would have had this sooner but i wrote all the comments in with the code explaining what i have done so you would not be lost in what i have done here in my example

Edited by jcanaway: n/a

0

Hi,

Thanks for the replies. Not your faults obviously but you did not understand what i meant. Solved now thou.

Thanks for your time and effort :)

Edited by genieuk: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.