1

How do I keep the selected item after page refreshed ,i have a language in the multiple select,

i am try now this with java script here is my code its not working ,can any one guide me how to make it selected

my html code

<div class="control-group">
<label for="textfield" class="control-label">Language</label>
<div  class="controls">
    <select name="myLanguage" id="myLanguage" multiple="multiple">
        <option value="English,">English,</option>
        <option value="Arabic,">Arabic,</option>
        <option value="Hindi,">Hindi,</option>
        <option value="Malayalam,">Malayalam,</option>
        <option value="Danish,">Danish,</option>

    </select>
</div>
</div>

javascript

 document.getElementById('myLanguage').value = "<?php echo $_GET['language'];?>";
1
  • do you really want a multiple select? in that case you have to set the name as an array and only use the multiple attr. <select name='myLanguage[]' id='myLanguage' multiple>. And if it is really multiple you need another solution. Commented May 31, 2014 at 13:19

5 Answers 5

1

If you use jQuery use this:

$(document).ready(function(){
    $('#myLanguage').val("<?php echo $_GET['language'];?>");
});
Sign up to request clarification or add additional context in comments.

Comments

0

Javascript doesn't use a session variables, so adding the value to the session isnt possible. Still you can do two things:

Option 1: Add the value to a cookie with Jquery:

Set: $.cookie("test", 1);

Read: var value = $.cookie("test");

See more: How do I set/unset cookie with jQuery?

Option 2: Post the value to the new page and request on that one:

Reading the value can be done with :

Read: $_REQUEST["my_variable"];

Note: reading value can be done like:

$("#myLanguage option:selected").text();

Comments

0

If you're using HTML5 as your doctype you can easily store values in the localStorage.

Reference: http://www.w3schools.com/html/html5_webstorage.asp

To set it:

localStorage.setItem("variable_name", variable_value);

To get it:

localStorage.variable_name

To remove it:

localStorage.removeItem("variable_name");

You can also use js cookies with jquery.cookie plugin, if you prefer.

Comments

0
As mentioned in the comments, when using the multiple attribute, 
the name attribute must be an array such as:

<div class="control-group">
    <label for="myLanguage[]" class="control-label">Language</label>
    <div  class="controls">
        <select name="myLanguage[]"  multiple="multiple"> 
            <option value="English,">English,</option>
            <option value="Arabic,">Arabic,</option>
            <option value="Hindi,">Hindi,</option>
            <option value="Malayalam,">Malayalam,</option>
            <option value="Danish,">Danish,</option>
        </select>
    </div>
</div>

javascript // Note: I'm not so sure on the following syntax

   var myLanguage[] = document.elements('myLanguage[]').value;

Comments

0
<?php
$languages = array(
  'en' => 'english',
  'vi' => 'vietnamese',
  'cn' => 'chinese'
);

if (isset($_GET['lang']) AND array_key_exists($_GET['lang'], $languages))
{
    include './lang/' . $languages[$_GET['lang']] . '.php';
}
else
{
    include './lang/english.php';

}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.