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. My target: after selection from Dropdown list, I am getting the value by 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.
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
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. See URLSearchParams
example below.
xhttp.send(new URLSearchParams({lang:lang}));
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.