mattegener
mattegener

Reputation: 42

Manipulating "selected" option with PHP

I'm creating a fairly basic form, and one of my requirements is to check and see if the values are valid upon submit, and if they are, but others aren't they should default to the original value. For some reason, mine keeps setting the last value in the option as selected, regardless of which option is clicked. Probably an obvious mistake somewhere, I just can't seem to see it. Here's the code.

<select name="degree">
<option value="" selected="<?php if ($_GET['degree'] === "false"){echo "selected";}?>">Select a Major</option>
<option value="Arts" selected="<?php if($_GET['degree'] === "Arts"){echo "selected";}
else{echo "false";}?>">Arts</option>
<option value="Science" selected="<?php if($_GET['degree'] === "Science"){echo "selected";}
else{echo "false";}?>">Science</option>
<option value="Commerce" selected="<?php if($_GET['degree'] === "Commerce"){echo "selected";}
else{echo "false";}?>">Commerce</option>
<option value="Economics" selected="<?php if($_GET['degree'] === "Economics"){echo "selected";}
else{echo "false";}?>">Economics</option>
<option value="Engineering" selected="<?php if($_GET['degree'] === "Engineering"){echo "selected";}
else{echo "false";}?>">Engineering</option>
<option value="Computer Science" selected="<?php if($_GET['degree'] === "Computer Science"){echo "selected";}
else{echo "false";}?>">Computer Science</option>
<option value="Music" selected="<?php if($_GET['degree'] === "Music"){echo "selected";}
else{echo "false";}?>">Music</option>
<option value="Business" selected="<?php if($_GET['degree'] === "Business"){echo "selected";}
else{echo "false";}?>">Business</option>
</select>

Upvotes: 0

Views: 163

Answers (2)

Lorenz Meyer
Lorenz Meyer

Reputation: 19935

Your problem is with HTML, not php. In fact, an option is selected, when it has the attribute selected, independent of its value.

<option selected>, <option selected="selected"> and <option selected="false"> are all selected. Only <option> is not selected.

You need therefore to change like this:

<option value="Arts"<?php if($_GET['degree'] === "Arts"){echo ' selected="selected"';}?>">Arts</option>

Now about the style

  • Do not repeat yourself (DRY)
  • indent your code
  • and specifically for PHP: do not change more often as necessary between PHP and HTML.

This would look somehow cleaner:

echo '<select name="degree">';
$options = array(
    'Select a Major' => '',
    'Science' => 'Science',
    ...
);
foreach($options as $display => $value){
    echo '<option value="'.$value.'"';
    if($_GET['degree'] == $value) echo 'selected="selected"';
    echo '>'.$display.'</option>';
}
echo '</select>';

Upvotes: 2

medCoder
medCoder

Reputation: 231

You should write a function telling you which option to set as "selected"

<?php 
function isOptSelected($opt){
    return ($_GET['degree'] == $opt)? ' selected="selected"': '';
}
?>

Further in the HTML code :

<select name="degree">
<option value="" <?php echo isOptSelected('');?> >Select a Major</option>
<option value="Arts"  <?php echo isOptSelected('Arts');?> >Arts</option>
<option value="Science"  <?php echo isOptSelected('Science');?> >Arts</option>
...

Upvotes: 0

Related Questions