0

I would like to know when the page reload with something like that : page.php?name=Andre

I would like the name "Andre" to be the default select item.

Because at the moment each time I reload I get the first element that was in the select option thing.

I try something like > but it doesn't work. Is there a easy way to do that?

  • possible duplicate of [How to set default value for HTML select element?](http://stackoverflow.com/questions/3518002/how-to-set-default-value-for-html-select-element) –  Mar 18 '14 at 01:22

4 Answers4

0

You can use the "selected" attribute inside of the option tag to designate which option is selected.

See http://www.w3schools.com/tags/tag_option.asp for more details.

MrWizard54
  • 515
  • 2
  • 8
  • 20
0

You could use jQuery/Javascript is faster but since you want PHP & HTML, try the following: Make a select list(if that's what you wanted):

<select name="mySelect">
<option value="Option1">1</option>
<option value="Option2">2</option>
<option value="Option3">3</option>
</select>

Then the PHP Code could go like this:

if($POST['mySelect'] == 'Option1') {
    print '<script language="Javascript">document.location.href="page.php?name=Andre";</script>';
}
0

You can try the following code:

Assuming that your list is fetched from an array or result set:

<select name="sel">
     <?php
     foreach ($options as option) {
         echo "<option value=\"$option\" " . ($option == $_GET['name'] ? "selected=\"selected\"" : "") . ">$option</option>";
     }
     ?>
</select>
0

You can also achieve this with jQuery

function getURLParameter(param) {
    return decodeURIComponent((new RegExp('[?|&]' + param + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
}

$(function(){
    $('option[value="' + getURLParameter('page') + '"]').prop('selected', 'selected');
});
vbn
  • 789
  • 1
  • 6
  • 16