Skip to content


How to set the selected option of <select> tag from the PHP

There is a few ways to do this. The easiest way to do this is in fact really simple, you just need to add a keyword “selected” to the option that you want selected.
For example, if You have something like this:

1
2
3
4
5
<select>
   <option value="1">Apple</option>
   <option value="2">Samsung</option>
   <option value="3">HTC</option>
</select>


If we want to have “Samsung” selected:

1
2
3
4
5
<select>
   <option value="1">Apple</option>
   <option selected value="2">Samsung</option>
   <option value="3">HTC</option>
</select>

Pretty easy, don’t you thing so 🙂

Now next thing that we need to do is to make our <select> submit so we could arrange the page accordingly.
To do that, we need to put it inside a <form>, like this:

1
2
3
4
5
6
7
<form>
   <select id="company" name="company"> 
      <option value="1">Apple</option> 
      <option value="2">Samsung</option> 
      <option value="3">HTC</option> 
   </select> 
</form>

Depending on your needs and requirements you may need some extra parameters set to the <form> tag, but for our example this is just fine
You can notice I added “id” and a “name” to the <select> tag, we will you these names to access the value at the backend.

Now for the fun part, let’s add some basic PHP code and bring our example to life.

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
 
(isset($_POST["company"])) ? $company = $_POST["company"] : $company=1;
 
?>
 
<form>
<select id="company" name="company">
<option <?php if ($company == 1 ) echo 'selected' ; ?> value="1">Apple</option>
<option <?php if ($company == 2 ) echo 'selected' ; ?> value="2">Samsung</option>
<option <?php if ($company == 3 ) echo 'selected' ; ?> value="3">HTC</option>
</select>
</form>

And that’s it. It has no real use, but it is good to get a grip of how the things work.

Hope someone finds this useful, and also that someone can contribute and make the example better.

Posted in PHP.


17 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Herman Nz says

    NB:does this work? I got trouble of inserting the code:

    Category:
    Pilih Kategori …
    <option value=”Profile1″>Profile>Visi dan Misi
    Profile>Sejarah Singkat
    ……………………. etc etc etc

  2. WIS3 says

    You have an error in your code. For autoselect the current value from php, you have in all 3 select ‘$company=1’ but that’s wrong. You need to have ‘$company= { value of the select}’.

    So you example shiuld be:

    <option value=”1″>Apple
    <option value=”2″>Samsung
    <option value=”3″>HTC

  3. WIS3 says

    OOps, my code has not been submittet completely, your system has stripped off some tag’s.

    Anyway, inside your select, the php code should be:

    .. if ($company == 1 ) ..
    .. if ($company == 2 ) ..
    .. if ($company == 3 ) ..

  4. no says

    Uhhh try this.

    if (isset($_POST[“text_content”])) { $scolorf = trim($_POST[“text_content”]); } else { $scolorf=0; };
    ?>

    <option value=”0″>–Select–
    <option value=”1″>Negro
    <option value=”2″>Azul
    <option value=”3″>Verde
    <option value=”4″>Rojo

  5. Nikola Vasiljevski Nikola Vasiljevski says

    You are right. I corrected the code. Thanks!

  6. Murdock says

    Hay que dejar un espacio entre selected y el apóstrofe: echo ‘selected’;. De no hacerlo, al imprimir, las palabras selected value quedan pegadas y no funciona. Gracias! me sirvió el código.

  7. Qupa says

    Thankyou!!

  8. Rahul says

    help if the value=”Jan” then what should be the code

  9. Rahul says

    or what should be in the case of a constant

  10. Germain says

    Thank you very much for your help from France ! 🙂

  11. kajol gupta says

    Thanks a lot sir….

  12. Nikola Vasiljevski Nikola Vasiljevski says

    Well I guess that you code got messed up on posting because there are to many error.
    So it’s hard for me to identify the error that is troubling you.

    Can you, please, sent the code to me by mail and I will check.

    Regards

  13. Nikola Vasiljevski Nikola Vasiljevski says

    The code should be, pretty much the same, just instead of comparing number you should compare strings to find out what should be selected.

  14. Nikola Vasiljevski Nikola Vasiljevski says

    Thanks, I updated the code on the page as you suggested.

  15. Nicky says

    What if, when you leave the page, and go back, you want the option you selected to now be shown instead of the default value? What should the php code be then please?

  16. Nikola Vasiljevski Nikola Vasiljevski says

    Well you would need to save the selected value somehow and use it when opening the page. You could read it from the database, store it in a file, use query string..
    Here is a really simple example, using file. Keep in mind this is just to show the concept 🙂

    test.php
    <?php
    $file = 'company.txt';
    $company = file_get_contents($file);
    ?>

    <form method="POST" action="test2.php">
    <select id="company" name="company">
    <option <?php if ($company == 1 ) echo 'selected' ; ?> value="1">Apple</option>
    <option <?php if ($company == 2 ) echo 'selected' ; ?> value="2">Samsung</option>
    <option <?php if ($company == 3 ) echo 'selected' ; ?> value="3">HTC</option>
    </select>
    <input type="submit" name="search" value="Next page"/>
    </form>

    test2.php
    <?php
    (isset($_POST["company"])) ? $company = $_POST["company"] : $company=1;

    $file = 'company.txt';
    file_put_contents($file, $company);
    ?>

  17. Max says

    What if you want to do this through a query string?



Some HTML is OK

or, reply to this post via trackback.