简体   繁体   English

select 选项 XMLHttpRequest 用于语言选择

[英]select option by XMLHttpRequest for language selection

Please help me to work out this idea: I want to select language and refresh the page after selection and it should change the laguage for present page.请帮我解决这个想法:我想要 select 语言并在选择后刷新页面,它应该更改当前页面的语言。 My target: after selection from Dropdown list, I am getting the value by document.getElementById('language');我的目标:从下拉列表中选择后,我通过 document.getElementById('language'); 获取值and want to send this value to next page, named ajax_lang.php and set in that page the SESSION and after that want to get the Session to refresh the page with Session laguage. and want to send this value to next page, named ajax_lang.php and set in that page the SESSION and after that want to get the Session to refresh the page with Session laguage.

I have copied the code which i tried.But failed.我已经复制了我尝试过的代码。但是失败了。 Please help me to work it out.请帮我解决。 thanks in advance.提前致谢。 part 1.php code for session start: part 2: javascript part 3. html code part 4: ajax_lang.php part 1.php code for session start: part 2: javascript part 3. html code part 4: ajax_lang.php

if(!isset($_SESSION)) 
     { 
         session_start();
                 
     }?>





<div style="margin-top:39px;background: #e9e9e9;margin-left:158px">
                        <select style="width: 40px;height: 40px;border-radius: 50%;width: 40px;
                                border: none;position: absolute;margin-top: 0px;" 
                                name="language" id="language" onchange="loadDoc(this.value)">
                              <option value=""><?php echo $_SESSION['lang'];?></option>
                              <option value="ru">RU</option>
                              <option value="en">EN</option>
                              
                        </select> 
                  </div>



<script>function loadDoc() {
      const xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          document.getElementById("language").innerHTML =
          this.responseText;
        }
      };
      //xhttp.open("POST", "ajax_lang.php");
      xhttp.open("POST", "ajax_lang.php");
         var select = document.getElementById('language');
         var lang = select.options[select.selectedIndex].value;
         console.log(lang); // en

       xhttp.send({lang:lang});
    }
    //loadDoc();
    </script>



<?php 
if(!isset($_SESSION['lang'])){
session_start();
$_SESSION['lang']=$_POST['lang'];
}


?> ```

XMLHttpRequest.send does not take a plain object as a parameter, it takes either a string or a URLSearchParams object. XMLHttpRequest.send不采用普通的 object 作为参数,它采用字符串或URLSearchParams object。 See URLSearchParams example below.请参阅下面的URLSearchParams示例。

xhttp.send(new URLSearchParams({lang:lang}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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