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.
3 Answers 3
There is so much wrong with this approach, I don't really know where to start :) But here is a possible solution:
In the ajax.php set a variable before lang.php include like so "$isAjax = true;", then in the lang.php make the whole "script" part condionnal to that variable
<?if(!$isAjax){?>
<script>....</script>
<?}?>
That's it ! You don't need the javascript lang variables again when you call ajax.php as these got already loaded in index.php.
Or:
Make one "lang.php" with ALL the lang dependent data, ALSO the ones you use in JS. Then include them in all pages and on the main pages ( not ajax ) include an other file, let's say "langJS.php" with contents like this:
<script>
var errorMessage = <?=$lang['JSerrorMessage']?>;
var thankyouMessage = <?=$lang['JSthankyouMessage']?>;
...
</script>
2 Comments
You can force a data type in your ajax call in order to ask jQuery to consider your ajax result as HTML (with script to evaluate). You have to use dataType setting.
$.ajax({
url: 'ajax.php',
dataType: 'html',
type: 'POST',
success: function(data) {
alert(data);
},
error: function() {
alert(errorMessage);
}
});
More details at http://api.jquery.com/jQuery.ajax/
Comments
The problem is that you are mixing lot of things and you lost the picture. The error is that you are outputting a text before the json_encode function in php. This means that jquery ajax function cannot intelligent-guess the content type of the answer that is plain text or is a malformed json as you wish.
Other error is that error: function() { } in jquery ajax is not called if you are not returning something different from status : 200OK from the web server
You use lang.php for both the javascript text and the php variables that are displayed on the page, this could be fine, or at least this is not an error itself.
There are multiple solution to your problem, one is the one answered by @minychillo A cleaner solution would be to separate the file for js messages and php ( used in dynamic pages ) messages.
I would do a lang.js
var errorMessage = 'message1';
var thankyouMessage = 'message2';
that i would include in the page with
<script src="lang.js" />
And i would leave the php file without the script part.
<?php ?>tags, then the content will be echoed out. Also, are there<script>tags in your index.php?