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
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
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
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 ) ..
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
You are right. I corrected the code. Thanks!
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.
Thankyou!!
help if the value=”Jan” then what should be the code
or what should be in the case of a constant
Thank you very much for your help from France ! 🙂
Thanks a lot sir….
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
The code should be, pretty much the same, just instead of comparing number you should compare strings to find out what should be selected.
Thanks, I updated the code on the page as you suggested.
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?
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);
?>
What if you want to do this through a query string?
Very nice, works like a sweet charm. Exactly what I was looking
Its working 🙂
Thanks you
It works for what I wanted to do! Thanks!
Thanks – works like a charm
anyone still here?
Yes 🙂 Why do you ask?
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!
Funciona muy bien. Gracias
this way, the server will be checking condition 3 times; why not do it like :
Apple
Samsung
HTC
Apple
Samsung
HTC
Apple
Samsung
HTC
I know, i am repeating almost same thing but if we do like this, the system wont be checking remaining conditions if it get true on first or second.
how can i use this in inside php