user1060817
user1060817

Reputation: 153

Selecting an Option in an HTML Select

I have an HTML select with several options. One of them is selected by default, but I would like to be able to change which one is selected. I found out how to do it using javascript, but I would like to do it using PHP as the information used to set it is stored in a PHP $_SESSION variable.

Upvotes: 0

Views: 219

Answers (3)

F21
F21

Reputation: 33391

Assuming the data is stored in $_SESSION['data'], then you can set the selected attribute.

<select name="dropdown">
  <option> <?php if ($_SESSION['data'] == "test"){ echo " selected "} ?> value="test">
   test
  </option>
</select>

Upvotes: 3

Benjamin Carlsson
Benjamin Carlsson

Reputation: 610

<select>
    <option<?php if($condition_one)   print 'selected'; ?>>One</option>
    <option<?php if($condition_two)   print 'selected'; ?>>Two</option>
    <option<?php if($condition_three) print 'selected'; ?>>Three</option>
</select>

Upvotes: 1

Caimen
Caimen

Reputation: 2619

You'll need to learn PHP, I recommend starting here.

http://www.w3schools.com/php/

Start Googling around for PHP tutorials. After you learn basic PHP, this will be easy for you.

Upvotes: 0

Related Questions