Jump to content

[SOLVED] How to make dropdown list stay on default value?


ted_chou12

Recommended Posts

I'm not sure if I'm understanding. If it's a javascript solution you're asking about, this implies that your page is not reloading. If your page is not reloading, your select box will not change from the selected component. If your page is reloading, you really need to modify your form to "remember" the selected value via PHP. Here's a simple example:

 

<?php
$val = 0;
if (isset($_POST['dropdown']))
{
  $val = $_POST['dropdown'];
  echo "You have chosen {$val}";
}
?>

<form name="my_form" action="" method="post">
<select name="dropdown" onchange="this.form.submit()">
<?php
$val = isset($_POST['dropdown']) ? $_POST['dropdown'] : 0;
$options = array(1,2,3,4,5);
foreach ($options as $o)
{
  echo "<option value=\"{$o}\"";
  echo $o == $val ? ' selected="selected"' : '';
  echo ">{$o}</option>\n";
}
?>
</select>
</form>

Link to comment
Share on other sites

sorry, maybe I didn't explain myself well enough, what I mean is without reloading the page, the dropdown list stays on the same value. Eg. when you reply to this thread, u can see a "change color" dropdown, and whatever color u click, it still stays on "change color".

Thanks

Ted.

Link to comment
Share on other sites

sorry, maybe I didn't explain myself well enough, what I mean is without reloading the page, the dropdown list stays on the same value. Eg. when you reply to this thread, u can see a "change color" dropdown, and whatever color u click, it still stays on "change color".

Thanks

Ted.

 

Well, if the page doesn't reload, by default a select box stays on whatever value you select.

Link to comment
Share on other sites

Sorry for not being clear, I mean eventhough you click on any value, it applies to the textarea, but the dropdown list still stays on "Change Color" without refreshing the page.

*example, if you reply or post new threads on phpfreaks, the dropdown for change color in the textarea works this way.

Thanks

 

Link to comment
Share on other sites

Sorry for not being clear, I mean eventhough you click on any value, it applies to the textarea, but the dropdown list still stays on "Change Color" without refreshing the page.

*example, if you reply or post new threads on phpfreaks, the dropdown for change color in the textarea works this way.

Thanks

 

Ah, so you are wanting the dropdown to stay on the initial option after selection? You just need to have a simple javascript function something like this:

 

Javascript:

function resetSelect(ele)
{
  ele.selectedIndex = 0;
}

 

HTML:

<select name="my_select" onchange="resetSelect(this);">
<option value="">Choose One</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

Link to comment
Share on other sites

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.