hilary clinton hilary clinton - 5 months ago 9
PHP Question

how to select a specific option when a condition exists

I want to select the specific option in drop down list when a condition is existed.I set the Session in php and if the combo box has the value of

1
, it will be shown the option with value
1
. I mean if session is
1
, select the option with value of
1
, if session is
2
, select the option with vlaue of
2
, and so on... . I want to set automatically select(I see the changes) with session in
php
.
sth like blewo:

<select id="sel" >
<option value='1'>one</option>
<option value='2'>two</option>
<option value='3'>three</option>
</select>
<?php $_SESSION['num']='1'; ?>
<script>
//must be shown the option with value of `1`.
<script>

Answer

Try this .

<select id="sel" >
  <option value='1'>one</option>
  <option value='2'>two</option>
  <option value='3'>three</option>
</select>
 <?php $_SESSION['num']='1'; ?>
<script>

    //set local variable value so that I don't have to do it for each of the following ways to do this.
    var num =  "<?php echo $_SESSION['num']; ?>";

    //normal javascript 
    document.getElementById("sel").value = num;

    //using jQuery  
    $("#sel").val(num);

<script>

Or this try vikingmaster's way

Comments