0

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();
} 
JohnDoe
  • 13
  • 5
  • 1
    Could we see `loadIn` – IsThisJavascript Feb 08 '18 at 16:26
  • I'm confused on the issue. Maybe some more code, like the javascript parts, will shed some light on what you are doing. Also, the php code that the ajax is hitting for its specific data return (json? html?). Also the 'exact' error text. – IncredibleHat Feb 08 '18 at 16:31
  • Did you put `require_once('./inc/lang/'.$_SESSION["lang"].'.php');` into the PHP files which respond to the ajax request? There's not enough code here to either reproduce the issue or least cover the areas of code which appear to be going wrong. It's not clear what you replace this form part with or how it's generated. – ADyson Feb 08 '18 at 16:33
  • `require_once('./inc/lang/'` that's only going to work on paths at root since `./` effectively means "relative to *this* directory" - so if your working directory is `/sett/badgers` then your `inc` dir would be `/sett/badgers/inc` ... drop that first `.` to make it relative to the docroot. – CD001 Feb 08 '18 at 16:36
  • Added loadIn to the mainpost. Errormessage: Notice: Undefined variable: mainloginPage in /var/customers/webs/JohnDoe/isle/inc/templates/lostPW.php on line 2 – JohnDoe Feb 08 '18 at 19:10
  • well I guess that means your variable $mainLoginPage is not declared in the lostPW.php file. So you have to declare it somehow and give it the correct values. Does it come from the language file? If so then you have to include the language file in that script. – ADyson Feb 08 '18 at 22:58
  • @ADyson exactly. But I wanted to prevent it. The language file includes all words / texts for the homepage. And loading it everytime, just to switch a
    input is not the smartest way..
    – JohnDoe Feb 08 '18 at 23:42
  • well, you need it, so you've got to include it. Including files is not a prohibitively expensive operation (in performance terms) - and it's common for dozens to be invoked on every request when using certain PHP frameworks. See https://stackoverflow.com/questions/4890825/whats-the-performance-cost-of-include-in-php . If you just need to include one file, I don't think you need to have anything to worry about. Just use it and solve your problem. – ADyson Feb 09 '18 at 00:15

0 Answers0