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:

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


If we want to have “Samsung” selected:

<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:

<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

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.

<?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

25 thoughts on “How to set the selected option of <select> tag from the PHP

  1. 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. 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. 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. 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. 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.

  6. 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

  7. 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?

  8. 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);
    ?>

  9. Nikola Vasiljevski, Thank you very much. By the simplicity of your example, I could understand how to handle my php page. Thanks a lot, and keep sharing! Hugs!

Leave a Reply

Your email address will not be published. Required fields are marked *


Recent Posts

GiottoPress by Enrique Chavez