I'm using a simple Javascript/PHP script to create a Ajax function on my website. The problem is; My config.php (included at top of my index.php) includes a site language script with different languages like English and German. It works fine on the base of my index.php, but it somehow isn't included in the Ajax pages. So I included the Language script inside of my AjaxExamplePage.php, and than it worked. But the problem is, it doesn't share the same cookies, being set once the user changes their language.
That means that, once the user changes his/her language on the base of the website (static HTML, not a Ajax call) it only applies to the static HTML, and not to the Ajax page.
Example line: <?php echo $lang["German"]; ?>
Output on static HTML: Deutsch
Ouput on Ajax Page: German
- Because it is still in English, after the user changes their language to German.
My Goal:
Once the user changes his/her language on the static HTML, it should also apply to the Ajax page. So if the user changes their language to German, than the Ajax page should be in German as well.
Javascript (Short version):
function loadPage(url)
{
url=url.replace('#','');
$('#loading').css('visibility','visible');
$.ajax({
type: "POST",
url: "pageContent_loader.php",
data: 'page='+url,
dataType: "html",
success: function(msg){
if(parseInt(msg)!=0)
{
$('#pageContent').html(msg);
$('#loading').css('visibility','hidden');
$('html, body').animate({ scrollTop: 0 }, 0);
}
}
});
}
pageContent_loader.php:
<?php
if(!$_POST['page']) die("0");
$page = $_POST['page'];
if(file_exists('content/general/'.$page.'.php'))
echo file_get_contents('content/general/'.$page.'.php');
else echo 'This page does not exist.';
?>
Language.php
session_start();
header('Cache-control: private');
if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];
// register the session and set the cookie
$_SESSION['lang'] = $lang;
setcookie("lang", $lang, time() + (3600 * 24 * 30), "/", ".domain.com");
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}
switch ($lang) {
default:
$lang_file = 'english.php';
case 'en':
$lang_file = 'english.php';
break;
case 'nl':
$lang_file = 'dutch.php';
break;
case 'fr':
$lang_file = 'french.php';
break;
case 'de':
$lang_file = 'german.php';
break;
}
require_once $_SERVER['DOCUMENT_ROOT'] . "/parts/languages/mainlang/".$lang_file;
(Domain.com is an example) Does anyone know how to achieve this?