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'];?>";
steven
  • 4,868
  • 2
  • 28
  • 58
Xavi
  • 2,552
  • 7
  • 40
  • 59
  • 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. ` – steven May 31 '14 at 13:19

5 Answers5

1

If you use jQuery use this:

$(document).ready(function(){
    $('#myLanguage').val("<?php echo $_GET['language'];?>");
});
dbucki
  • 454
  • 3
  • 7
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();
Community
  • 1
  • 1
K Mich
  • 104
  • 4
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.

lesssugar
  • 15,486
  • 18
  • 65
  • 115
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;
user3629249
  • 16,402
  • 1
  • 16
  • 17
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';

}
?>
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
xsd
  • 1