简体   繁体   中英

Set selected value on dynamic dropdown list

I have the below code, so the user can select the language he desire:

<label style="float: left; width: 50%;" for="system_language">Select Language:</label>
        <select id="system_language" class="selectbox float-right" onChange="switchLanguageLogin();  ">
        <? echo getLanguageList(); ?>

    </select>

This is the function of the languages!

function loadLanguage($lng, $units = '')
{
    global $ms, $la, $gsValues;

    // always load main english language to prevet error if something is not translated in another language
    include ($gsValues['PATH_ROOT'].'lng/english/lng_main.php');

    // load another language
    if ($lng != 'english')
    {
        $lng = $gsValues['PATH_ROOT'].'lng/'.$lng.'/lng_main.php';

        if (file_exists($lng))
        {
            include($lng);
        }
    }

Added the Language Function

function getLanguageList()
{
    global $ms, $gsValues;

    $result = '';
    $languages = array();

    $q = "SELECT * FROM `gs_system` WHERE `key`='LANGUAGES'";
    $r = mysqli_query($ms, $q);
    $row = mysqli_fetch_array($r);

    $languages = explode(",", $row['value']);

    array_unshift($languages , 'english');

    foreach ($languages as $value)
    {
        if ($value != '')
        {
            $result .= '<option  value="'.$value.'">'.ucfirst($value).'</option>';  
        }
    }

    return $result;
}

The first (and default) option on the dropdown menu is English. The problem is that if I choose Spanish, it translates to Spanish, but on the dropdowm menu it leaves the default value which is English. This concludes that the page is in Spanish, but the value from the dropdown shows "English".

How can i solve this?

You do not use your $lng variable in the global scope, so it is not visible for your function. A solution would therefore be to provide the selected language as parameter to the getLanguageList function and set the equal value as selected:

function getLanguageList($selected = 'english') {
    //...
    foreach ($languages as $value) {
        if ($value !== '') {
            $result .= '<option  value="'.$value.'" ' . ($selected === $value ? ' selected="selected"' : ''). '>' . ucfirst($value) . '</option>';  
        }
    }
    //...
}

Like this the selection is kept for the dropdown and therefore for the HTML. In your view you would then need to provide $lng and call <? echo getLanguageList($lng); ?> <? echo getLanguageList($lng); ?> <? echo getLanguageList($lng); ?> .

Check in your init.php file should have a comment

// gets language from cookies

If there is not such comment, write it just before MySQL connection and add the following code after it

if (isset($_COOKIE['gs_language']))
{
    $gsValues['LANGUAGE'] = $_COOKIE['gs_language'];
}
else
{
    $expire = time() + 2592000;
    setcookie('gs_language', $gsValues['LANGUAGE'], $expire, '/');
}

// puts selected language into cookies
if (isset($_GET['lng']))
{
    $gsValues['LANGUAGE'] = $_GET['lng'];
    $expire = time() + 2592000;
    setcookie('gs_language', $gsValues['LANGUAGE'], $expire, '/');
}

Then go to fn_common.php file and change the getLanguageList function to

function getLanguageList()
{
    global $ms, $gsValues;

    $result = '';
    $languages = array();

    $q = "SELECT * FROM `gs_system` WHERE `key`='LANGUAGES'";
    $r = mysqli_query($ms, $q);
    $row = mysqli_fetch_array($r);

    $languages = explode(",", $row['value']);

    array_unshift($languages , 'english');

    $currentLang = $gsValues['LANGUAGE'];

    foreach ($languages as $value)
    {
        if ($value != '')
        {
            $result .= '<option  value="'.$value.'" ' . ($currentLang === $value ? ' selected="selected"' : ''). '>' . ucfirst($value) . '</option>';
        }
    }

    return $result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM