2

So I have this guy:

<!-- Combobox -->
		<table>
		<br>Is your kitten active or lethargic?:
						<select name="Activity">
							<option value="Active">Active</option>
							<option value="Lethargic">Lethargic</option>
						</select>
		</table>

With two options, now somewhere above is a guy that does this:

$Activity = $_POST['Activity'];

For my radiobuttons I have a code that checks them based on who was inside a variable before I want to adapt it to do something similar on this combobox, can anyone help out? I am a beginner in php so dumb it down to 5 year old, even add dinosaurs into the mix if you want. But do help me comprehend it.

Oh and if its not too much to ask how can default the radiobuttons to male if the variable is empty or does not have the correct word its checking for?

My radiobutton code:

<!-- Radio Buttons -->
		  Gender:
		  <input type="radio" name="Gender" value="Male" <?php echo ($Gender=='Male')?'checked':'' ?>>Male &nbsp; &nbsp;
		  <input type="radio" name="Gender" value="Female" <?php echo ($Gender=='Female')?'checked':'' ?>>Female &nbsp; &nbsp;

1
  • Are you looking for just a way to mark an option as selected? If that's what you're looking for, on the <option> you just need to add the "selected" attribute, same as "checked" but for select boxes Commented Mar 24, 2015 at 0:40

2 Answers 2

3

You can use selected attribute on your option tag.

<option value="Active" <?php echo ($Activity =='Active')?'selected':'' ?> >Active</option>
<option value="Lethargic" <?php echo ($Activity =='Lethargic')?'selected':'' ?>>Lethargic</option>
Sign up to request clarification or add additional context in comments.

1 Comment

This is correct, disappointed in the lack of dinosaurs though, but thank you. any ideas on how to have a default selected? for this and radiobuttons?
1

To answer your first question, it works just the same as with radio buttons, except that you use selected instead of checked:

<!-- Combobox -->
<table>
<br>Is your kitten active or lethargic?:
    <select name="Activity">
        <option value="Active"<?php echo ($Activity == 'Active') ? ' selected' : ''; ?>>Active</option>
        <option value="Lethargic"<?php echo ($Activity == 'Lethargic') ? ' selected' : '' ?>>Lethargic</option>
    </select>
</table>

And to answer your second question about defaulting to male I would suggest something like this:

$Gender = isset($_GET['Gender']) ? $_GET['Gender'] : '';
if ( ! in_array($Gender, array('Male', 'Female')) {
  $Gender = 'Male';
}

This checks if $Gender is 'Male' or 'Female' and if neither is true it sets $Gender to 'Male'.

2 Comments

What if $Gender is empty?
Alright, this might give you a PHP 'Notice'. To be correct use $Gender = isset($_GET['Gender']) ? $_GET['Gender'] : ''. I'll update the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.