Jump to content

Select List: Assigning Variable and Making Sticky


doubledee

Recommended Posts

Okay, really newbie question, but for this code...

	<!-- Gender -->
	<label for="gender">Gender:</label>
	<select id="gender" name="gender">
		<option value="">--</option>
		<option value="F">Female</option>
		<option value="M">Male</option>
	</select>

 

1.) How do I assign a variable to this?

 

2.) How do I make this "sticky"?

 

 

Here is how I have usually done other form types...

<!-- First Name -->
<label for="firstName">First Name:</label>
<input id="firstName" name="firstName" type="text" maxlength="30"
	value="<?php if(isset($firstName)){echo htmlentities($firstName, ENT_QUOTES);} ?>" /><!-- Sticky Field -->
<?php
	if (!empty($errors['firstName'])){
		echo '<span class="error">' . $errors['firstName'] . '</span>';
	}
?>

 

 

Oh, by the way, at the top of my PHP file I have this code...

if ($_SERVER['REQUEST_METHOD']=='POST'){
	// Form was Submitted (Post).

	// Initialize Errors Array.
	$errors = array();

	// Trim all form data.
	$trimmed = array_map('trim', $_POST);

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

<label for="gender">Gender:</label>
<select id="gender" name="gender">
  <option value="">--</option>
  <option value="F"<?php (isset($gender) && $gender == "F") ? 'selected="selected"' : ''; ?>>Female</option>
  <option value="M"<?php (isset($gender) && $gender == "M") ? 'selected="selected"' : ''; ?>>Male</option>
</select>

Link to comment
Share on other sites

1. The same way as any other form element. $_POST['gender']

 

2. thorpe beat me to it.

 

EDIT: Although, he forgot echo's.

<label for="gender">Gender:</label>
<select id="gender" name="gender">
  <option value="">--</option>
  <option value="F"<?php echo (isset($gender) && $gender == "F") ? 'selected="selected"' : ''; ?>>Female</option>
  <option value="M"<?php echo (isset($gender) && $gender == "M") ? 'selected="selected"' : ''; ?>>Male</option>
</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.