Jump to content

Select box problems

Go to solution Solved by Barand,

Recommended Posts

Hi. I have 2 problems which I'm hopeing someone can help me with.


1. For the following script to change the pages language, I would like it so that whichever option selected is set on the page (i.e. if you select Euskal, the select box stays set as Euskal, and not Espanyol as it does at the moment)


      <form name="Language" action="" method="post" id="Language">

          <div class="btn-group">


          $uri = strtok($_SERVER["REQUEST_URI"],'?');;



          <span class="btn btn-primary">

          <select name="lang" onchange="javascript:change_langs(this.form)">

          <option value="Esp">Espanyol</option>

          <option value="Eng">English</option>

          <option value="Cat">Catalan</option>

          <option value="Bas">Euskal</option>

          <option value="Gal">Galego</option>







2. I have 2 dropdown lists in the following format:


<select class="form-control" name='category'>

<option <?php print ((isset($_GET["category"]) && ($_GET["category"]=="A"))?"selected='A'":""); ?> value='A'><?=getlg($lang,"A")?></option>

<option <?php print ((isset($_GET["category"]) && ($_GET["category"]=="b"))?"selected='b'":""); ?> value='b'><?=getlg($lang,"b")?></option>

<option <?php print ((isset($_GET["category"]) && ($_GET["category"]=="c"))?"selected='c'":""); ?> value='c'><?=getlg($lang,"c")?></option>



<select class="form-control" name='category'>

<option <?php print ((isset($_GET["category"]) && ($_GET["category"]=="D"))?"selected='D'":""); ?> value='D'><?=getlg($lang,"D")?></option>

<option <?php print ((isset($_GET["category"]) && ($_GET["category"]=="e"))?"selected='e'":""); ?> value='e'><?=getlg($lang,"e")?></option>

<option <?php print ((isset($_GET["category"]) && ($_GET["category"]=="f"))?"selected='f'":""); ?> value='f'><?=getlg($lang,"f")?></option>



However, I require just one dropdown list with catagories A and D as the main item. Catagories b & c should be a sub category of A, and e & f should be a sub category of D


Any help would be appreciated. Thanks
Link to comment
Share on other sites

  • Solution

1. Store selected language in a session variable and use the "selected" attribute to select the stored language

2. Use <optgroup> to group the subcats into categories

if (isset($_GET['lang'])) {
    $_SESSION['lang'] = $_GET['lang'];  // store newly selected lang
if (!isset($_SESSION['lang'])) {
    $_SESSION['lang'] = 'Esp';          // set default if not yet set

$langs = array(
            "Esp" => "Espanyol",
            "Eng" => "English",
            "Cat" => "Catalan",
            "Bas" => "Euskal",
            "Gal" => "Galego",
$langOpts = '';
foreach ($langs as $k => $l) {
    $sel = ($k == $_SESSION['lang']) ? 'selected="selected"' : '';
    $langOpts .= "<option value='$k' $sel> $l</option>\n";

$cats = array (
            "A" => array ("b", "c"),
            "D" => array ("e", "f")
$catOpts = '';
foreach ($cats as $c => $subs) {    
    $catOpts .= "<optgroup  label='$c'>\n";
    foreach ($subs as $sc) {
        $catOpts .= "<option value='$sc'> $sc</option>\n";
        #$catOpts .= "<option value='$sc'>    $sc</option>\n";
    $catOpts .= "</optgroup>\n";
<style type="text/css">
option.subcat {
    padding-left: 25px;
    <select name="lang" onchange="javascript:change_langs(this.form)">
          <?= $langOpts ?>
    <select name="category">
        <option value=''>Select ...</option>
        <?= $catOpts ?>
    <input type="submit" name="btnSub" value="Submit">      
Link to comment
Share on other sites

I would make one slight tweak to what Barand provided. I would still use a session value to save the user's language selection, but I wouldn't use that variable when determining which language to use for the output. Instead I would determine separately 1) What language to use and 2) What is the user's selected language. Here's why:


The fact that a user has not selected a language may be an important piece of data. Yes, you should have a default language to fall back on, but sometimes you may need confirmation that the user has selected a language. For example, if you just showed pages in a specific language that the user is not familiar with it may be difficult for them to understand what items to select to change it. Instead, you may provide them with a prompt when they visit your site if they have no solved selection that includes text in all the supported languages asking them to make a selection.


So, I would do something such as this:

//Set the default presentation language
$presLang = 'Esp';
if (isset($_GET['lang'])) {
    $_SESSION['lang'] = $_GET['lang'];  // store newly selected lang
if (isset($_SESSION['lang'])) {
    $presLang = $_SESSION['lang'];          // set presentation language if user selected

Then, use $presLang when determining which language to create output in. When showing a select list for the user to choose a language I would use the session value and add a "Select Language" option if they have not previously selected one.

$langOpts = "<option value=''>- Select a Language -</option>\n";
foreach ($langs as $k => $l) {
    $sel = ($k == $_SESSION['lang']) ? 'selected="selected"' : '';
    $langOpts .= "<option value='$k' $sel> $l</option>\n";
Edited by Psycho
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.

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.