Reputation: 1409
I have a problam that I dynalically add a class to div through php, and then checking if id "checkLang" has spacific class, but it desn't work :(
Maybe someone knows the problem? My html:
<div id="checkLang" class ="<?php echo $valoda ?>"><div>
and that div I check with
if ($('#checkLang').hasClass('RU')){
<?php include("lan/ru.php"); ?>
};
if ($('#checkLang').hasClass('LV')){
<?php include("lan/lv.php"); ?>
};
I don't know why, but both ifs include language php file.
But maybe the reason is because this script is in php file with header <?php header('Content-type: text/javascript'); ?>
I attach file like javascript file in index page like <script type="text/javascript" src="php/FormuValidacija.php" /></script>
I tried
if($_GET['lang']=="latviesu"){
include("php/lan/lv.php");
}
else($_GET['lang']=="krievu"){
include("php/lan/ru.php");
}
But doesn't work as well :(
P.S. Sorry if this is stupid question, i'm new whit this stuff, but willing to learn! :)
Upvotes: 0
Views: 1953
Reputation: 1304
Current language:
<div id="checkLang" class ="<?php echo $valoda ?>"><div>
Including your localization files:
<div class="ru" style="display:none;">
<?php include("lan/ru.php"); ?>
</div>
<div class="lv" style="display:none;">
<?php include("lan/lv.php"); ?>
</div>
Getting current language and showing corresponding div
<script type="text/javascript">
$(document).ready(function(){
var currLng = $('#checkLang').attr('class'); // get current language, i.e class of #checkLang
$('div.' + currLng).show(); // show div with corresponding class, i.e lang
});
</script>
However, its better to get language at server side and not to load unused files to client(i.e HTML)
P.S un labāk visus variablus, script failus etc saukt anglū valodā :)
Upvotes: 1
Reputation: 446
it includes your content because its generated server side already, so when the html site is transfered the php script is already rendnered while the javascript is handled client side, you would have need to adapt your try
you see this would work
<div id="checkLang" class ="<?php echo $valoda ?>"><div>
<div class="ru" style="display:none;">
<?php include("lan/ru.php"); ?>
</div>
<div class="lv" style="display:none;">
<?php include("lan/lv.php"); ?>
</div>
$(document).ready(function()
{
if ($('#checkLang').hasClass('RU')){
$('.ru').show();
}
if ($('#checkLang').hasClass('LV')){
$('.lv').show();
};
});
example here:
tough it would rendner all language content; you should consider doing the language selection within the php code to avoid extra traffic unless you want to switch language via javascript only without any server interaction.
regards jan
also as Arun P Johny stated
...your script is not added in a dom ready handler...
regards jan
Upvotes: 0
Reputation: 9304
Given my jsfiddle example here, you can clearly see that javascript is able to check that the given class is set on the div.
Both of the php files will be rendered no matter what, because the if check here is done on the client side javascript. if you wish to only render data from the php files once the check has been done on the client side. Then you have to fetch the data from the client side instead of serving it from the server side.
One way of doing this is through ajax. for example. you could create a php script that returns html contents based on a query, something like:
checklang.php?lang=RU
and in the javascript code you would have a request set up like this:
$.get('checklang.php?lang=RU', function(data) { //switch RU with a js variable so you can change between RU and LV programatically
$('#some-content-div').html(data);
});
Upvotes: 0
Reputation: 388316
Looks like your script is not added in a dom ready handler like
jQuery(function($){
alert($('#checkLang').length)//it should alert 1 not 0
if ($('#checkLang').hasClass('RU')){
<?php include("lan/ru.php"); ?>
};
})
Upvotes: 2