Jump to content

Recommended Posts

Hey guys, I was wondering how I can get my dropdown box to select a certain value based on a variable. If you do not know what I mean, let me give you an example:

 

<?php

$category = "Classic";

?>

<form name="blabla" method="post">

<select name="category">

<OPTION>Adult</OPTION>
<OPTION>Alcohol</OPTION>
<OPTION>Bar</OPTION>
<OPTION>Blonde</OPTION>
<OPTION>Brunette</OPTION>
<OPTION>Classic</OPTION>
<OPTION>Men</OPTION>
<OPTION>Sports</OPTION>
<OPTION>Women</OPTION></select>

</form>

 

In the example above, I would like "Classic" to be preselected in the dropdown box when the page loads, because $category = "Classic". If $category was = "Blonde", then I would obviously want "Blonde" to be preselected. Is there a simple way to do this?

 

Thank you!

Link to comment
https://forums.phpfreaks.com/topic/126896-solved-dropdown-selection-from-variable/
Share on other sites


<OPTION <?php if($var == adult){ echo selected="selected"; } ?>>Adult</OPTION>
<OPTION>Alcohol</OPTION>
<OPTION>Bar</OPTION>
<OPTION>Blonde</OPTION>
<OPTION>Brunette</OPTION>
<OPTION>Classic</OPTION>
<OPTION>Men</OPTION>
<OPTION>Sports</OPTION>
<OPTION>Women</OPTION>

 

Something like that going all the way down should do it, I believe. :]

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.