John Davidson

javascript - Variables don't keep values when ajax loaded

0 comments
Message:


PHP variables doesn't keep their values when AJAX loads a content.


index.php Here is my code in index.php:


<?php
if (empty($_GET["j"])) {@$_GET["j"] = "en";}
$lang = htmlspecialchars($_GET["j"]);

if (empty($_GET["as"])) {@$_GET["as"] = "home";}
$page = htmlspecialchars($_GET["as"]);

require_once("/scripts/header.php");
require("/scripts/languages.php");
?>

<div id="page">
<?php
if (file_exists("pages/".$page.".php")) {
include("pages/".$page.".php");
}
?>
</div>

<nav>
<a href="/cs/home" class="link">Domů</a>
<a href="/en/contact" class="link">Kontakt</a>
</nav>

<script src="/scripts/loader.js"></script>

.htaccess I need the URL to be rewrited by this rule:


RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*)$ index.php?j=$1&as=$2 [QSA]

languages.php - There I have some languages variables which I need to use in loaded content by ajax in #page div


<?php
if ($lang== "cs") {
$headline = "This is title in another language";
$text = "
<p>This is long text in another language.</p>
<p>This is second paragraph in another language.</p>
";
}
else {
$headline = "This is title";
$text = "
<p>This is long text.</p>
<p>This is second paragraph.</p>
";
}
?>

loader.js - Ajax loader:


$(document).ready(function() {
$('nav a.link').click(ChangePage);
});

function ChangePage() {
var Link = $(this).attr('href');
var Page = Link.substring(4);
var Lang= Link.substring(1, 3);
var Path = "/pages/" + Page+ ".php";

$.ajax({
url: Path,
type: "POST",
success: function(result){
$("#page").html(result);
document.title = $('#page_wrap').data('title');
},
error: function() {
alert("there is some error");
}
});

return false;
}

home.php - I have this files like home, contact etc. which I need to load into #page div


<div id="page_wrap" data-title="Home - Site Name">HOME</div>
<?php
echo "
<br>This is lang: ".$lang."
<br>Headline: ".$headline."
<br>Text: ".$text."
";
?>

If I load page for first time it works. After I load content by clicking on nav link the variable of $lang is undefined.. How can I fix it please? I've even tried php session, but it didn't work at all. Is there any way how to fix it?



My Answer:
't need to be declared with a specific data type in JavaScript. You can simply use the "var", "let", or "const" keywords to declare a variable and assign a value to it. JavaScript is a dynamically typed language, which means that variables can hold values of any data type and can be reassigned to different data types at any time.

For example:

javascript
var myVar = 10; // declaring a variable and assigning a number value
let myString = "Hello, world!"; // declaring a variable and assigning a string value
const PI = 3.14; // declaring a constant variable with a number value

myVar = "JavaScript"; // reassigning the variable to a string value


In the above example, "myVar" is initially assigned a number value, but it can be reassigned to a string value later on. The "let" keyword is used to declare variables that can be reassigned, while the "const" keyword is used to declare constants that cannot be reassigned.

Rate this post

3 of 5 based on 6200 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us