I have an application that contains language file "lang.php", "index.php" and ajax calling file "ajax.php". Lang file contains php and javascript variables which I include in "index.php" and "ajax.php" so I can change langugage with translating "lang.php" file.
Problem is that when i include "lang.php" in "ajax.php" because it contains javascript variables inside <script></script> tags and ajax interprets it like output. When I remove <script> tags and when "lang.php" only contains php variables, it works correctly. Here is the content of my files. How can I do this without removing javascript variables (because I need one langugage file per language).
index.php
include "lang.php";
function test()
{
$.ajax({
url: 'ajax.php',
type: 'POST',
success: function(data) {
//manipulating returned json;
},
error: function() {
alert(errorMessage);
}
});
};
<input type="button" onclick="test();">
lang.php
<script>
var errorMessage = "Error during ajax call";
</script>
<?php
$var1 = "Welcome";
$var2 = "Header";
?>
ajax.php
include "lang.php";
$array = array($var1, $var2);
echo json_encode($array);
die();
Thank you for your help.
<?php ?>tags, then the content will be echoed out. Also, are there<script>tags in your index.php?