简体   繁体   中英

Get selected text of dropdown menu without submit button

I have this drop down menu:

if(isset($_POST['ss'])){$_SESSION['ss'] = $_POST['ss'] ;}else{$_SESSION['ss'] = 'English';}


<select name = "selectlang" class="selectlang" onchange="location = this.options[this.selectedIndex].value;">
        <option  value='<?php $website_link ?>?lang=english' <?php if($_SESSION['ss'] == "English"){echo ' selected="selected"' ;} ?>>English</option>
        <option  value='<?php $website_link ?>?lang=swedish' <?php if($_SESSION['ss'] == "Swedish"){echo ' selected="selected"' ;} ?>>Swedish</option>
        <option  value='<?php $website_link ?>?lang=russian' <?php if($_SESSION['ss'] == "Russian"){echo ' selected="selected"' ;} ?>>Russian</option>
</select>
<input type="hidden" name="ss" id="lang_hidden" value="" />

this actually when I select one of languages it submit and added ?lang=..... in the url link. And it works good, the url changes how ever the drop down menu is always English.

How can i make it changes also to selected language ?

I have used this but it didnt work.

$(".selectlang").change(function(){
var inpvalue = $('.selectlang option:selected').text();
    $("#lang_hidden").val(inpvalue).val();
   //$("#lang_hidden").closest('form').trigger('submit'); //(i will explain this)
  });

I explain that line above, when I uncomment it and add the form tags

  <form method="POST" action="">
 the above html code
  </form>

Well the dropdown menu keeps the selected language but dont change the link with lang=..... and pages not translated.

How can I manage them both to keep selected drop down menu text and get link changed?

Thanks in advance.

 //wanna see my function language so let me know.

my function language and where im making session variables maybe the error is here.

   function setlanguage(){
    //$_SESSION['selectlang'] = '';
    //if(!isset($_SESSION['selectlang'])){$_SESSION['selectlang'] = 'English' ;}
    //if(!isset($_GET['lang'])){$_GET['lang'] = 'english' ;}
    if(isset($_GET['lang'])){
        switch ($_GET['lang']){
            case "english" :
                    $_SESSION['selectlang'] = 'English';
                  include("lang/english.php");
                  break;
            case "swedish" :
                    $_SESSION['selectlang'] = 'Swedish';
                   include("lang/swedish.php");
                   break;
            case "russian":
                    $_SESSION['selectlang'] = 'Russian';
                   include("lang/russian.php");
                 break;

                }
        }
    else if(!isset($_GET['lang']) and isset($_SESSION['selectlang'])){
        switch ($_SESSION['selectlang']){
            case "English" :
                    $_SESSION['selectlang'] = 'English';
                  include("lang/english.php");
                  break;
            case "Swedish" :
                    $_SESSION['selectlang'] = 'Swedish';
                   include("lang/swedish.php");
                   break;
            case "Russian":
                    $_SESSION['selectlang'] = 'Russian';
                   include("lang/russian.php");
                 break;
            default :
                    include("lang/english.php");
        }
    }
    else{include("lang/english.php"); $_SESSION['selectlang'] = 'English';}

   }

You use different session variable names

in php

$_SESSION['selectlang'] = 'Russian';

in HTML

$_SESSION['ss'] == "Russian"

When You redirect page, if You are sure, You save the value to session variable, then the problem might be in letter case.

value='<?php $website_link ?>?lang=english'

change

<?php if($_SESSION['ss'] == "English"){echo ' selected="selected"' ;} ?>

to

<?php if($_SESSION['ss'] == "english"){echo ' selected="selected"' ;} ?>

Try the below options.

 <option  value='<?php $website_link ?>?lang=english' <?php if($_SESSION['ss'] == "english"){echo "selected" ;} ?>>English</option>

or 

 <option  value='<?php $website_link ?>?lang=english' <?php if($_REQUEST['lang'] == "english"){echo "selected" ;} ?>>English</option>

try this to change your url without page load.

pageurl = 'yourpage.php?lang=inpvalue;
if(pageurl!=window.location){
    window.history.pushState({path:pageurl},'',pageurl);
}

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