5

When will I update my semester that time by default a checkbox to be checked how it possible ?

<select name="semester">
  <option value="">Select Semester</option>
  <option value="1">Semester 1</option>
  <option value="2">Semester 2</option>
  <option value="3">Semester 3</option>
</select>
Noman
  • 1,459
  • 2
  • 18
  • 38
Omar Faruk
  • 65
  • 4

6 Answers6

3

You can use the selected attribute.

<select name="semester">
     <option value="">Select Semester</option>
     <option value="1" selected="selected">Semester 1</option>
     <option value="2">Semester 2</option>
     <option value="3">Semester 3</option>
</select>
Dave
  • 3,073
  • 7
  • 20
  • 33
Ben Hillier
  • 2,126
  • 1
  • 10
  • 15
  • I want to do it by php not html such as Rahim is a studen of 1s semister I want to update his semister when I will update that time his semister by default checked semister 1st – Omar Faruk Sep 08 '16 at 10:59
  • @OmarFaruk I've made a generic PHP example and posted it as a separate answer. – Ben Hillier Sep 08 '16 at 11:32
2

If you want to select semester 2 so here you go:

HTML:

<select name="semester">
  <option value="">Select Semester</option>
  <option value="1">Semester 1</option>
  <option value="2" selected>Semester 2</option>
  <option value="3">Semester 3</option>
</select>

Jquery:

$("select option").each(function(){
  if ($(this).text() == "Semester 2")
    $(this).attr("selected","selected");
});
Noman
  • 1,459
  • 2
  • 18
  • 38
2

Please try this

<select name="semester">
            <option value="">Select Semester</option>
            <option value="1" <?php if($data['semester']==1){echo 'selected';}?>>Semester 1</option>
            <option value="2" <?php if($data['semester']==2){echo 'selected';}?>>Semester 2</option>
            <option value="3" <?php if($data['semester']==3){echo 'selected';}?>>Semester 3</option>
</select>
Sumon Hasan
  • 310
  • 3
  • 14
0

yes,it possible. you have to use selected in <option>

<select name="semester">
        <option value="">Select Semester</option>
        <option value="1" selected>Semester 1</option>
        <option value="2">Semester 2</option>
        <option value="3">Semester 3</option>
</select>

DEMO

Dave
  • 3,073
  • 7
  • 20
  • 33
0
<option value="1" <?= ($value=='1')?'selected':"" ?>>Semester 1</option>
<option value="2" <?= ($value=='2')?'selected':"" ?>>Semester 2</option>
<option value="3" <?= ($value=='3')?'selected':"" ?>>Semester 3</option>

you can get the value form database or session,

KBK
  • 375
  • 1
  • 4
  • 20
0

We really need your specific PHP code to help, but perhaps this generic example will show you how to do this when you have your data and your desired value:

<?php
    //Your array of data
    $myData = array(
        '1' => 'Semester 1',
        '2' => 'Semester 2',
        '3' => 'Semester 3');

    //The value you want to be selected
    $myValue = "2";
?>

<select name="semester">
    <option value="">Select Semester</option>
        <?php
            foreach($myData as $key => $value){
                echo ('<option value="'.$key.'"' );
                if( $myValue == $key){
                    echo( ' selected="selected" ');
                }
                echo ('>'.$value.'</option');
                echo "\n";
            }
        ?>
</select>
Ben Hillier
  • 2,126
  • 1
  • 10
  • 15