I started a new project and for the first time with language files. I import the language file with this:
if(!isset($_SESSION["lang"])) {
$_SESSION["lang"] = "en";
}
require_once('./inc/lang/'.$_SESSION["lang"].'.php');
Thats not a problem on a usual index.php:
<div id="login">
<form method="post">
<label for="usrName"><?php echo $mainloginPage['userName']; ?></label><br />
<input type="text" id="usrName" maxlength="25" autofocus />
<br />
<br />
<label for="usrPassword"><?php echo $mainloginPage['password']; ?></label><br />
<input type="password" id="usrPassword" />
<p class="center">
<a href="#" onclick="loadIn('lostPW')"><?php echo $mainloginPage['forgotpw']; ?>?</a>
<br />
<br />
<a href="#" onclick="loadIn('signUp')"><?php echo $mainloginPage['register']; ?></a>
</p>
<input type="submit" value="<?php echo $mainloginPage['login']; ?>" />
</form>
</div>
You see the $mainLoginPage - thats a variable from the language file. When I load another file with AJAX and replace this form part, the variables do not apply and I get errors for not defined variables.
The code that gets loaded in (/inc/templates/lostPW.php):
<form method="post">
<label for="mail"><?php echo $mainloginPage['mail']; ?>:</label><br />
<input type="text" id="usrMail" autofocus />
<p class="center">
<a href="#" onclick="loadIn('login')"><?php echo $mainloginPage['login']; ?>?</a>
<br />
<br />
<a href="#" onclick="loadIn('signUp')"><?php echo $mainloginPage['register']; ?></a>
</p>
<input type="submit" value="<?php echo $main['send']; ?>" />
</form>
The onclick events are just loading a function with a usual xhttp request.
How do I get the variables of this with ajax new loaded file?
loadIn function
function loadIn(file) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("login").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "/inc/templates/" + file + ".php", true);
xmlhttp.send();
}