csdn产品小助手 2018-06-09 14:37 采纳率: 0%
浏览 27

Ajax页面内的PHP

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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥30 用arduino开发esp32控制ps2手柄一直报错
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题
    • ¥15 Visual Studio问题
    • ¥20 求一个html代码,有偿