WPML |Get the current ICL_LANGUAGE_CODE with JS - javascript

I tried to retrieve the current ICL language with this code but it doesn't work.
var getLangCode = '<?php echo ICL_LANGUAGE_CODE; ?>';
alert(getLangCode);
Do you know how can I get the current language code with javascript ?
Thanks in advance !

You just need to place the quotes in the right places.
Remove quotes before <?php and after ?> and surround the PHP constant with double quotes.
var getLangCode = <?php echo '"' . ICL_LANGUAGE_CODE . '"' ; ?> ;
alert(getLangCode);

The accepted solution did not work for me. Actually, none of the solutions I've found which involved <?php or echo worked for me.
The workaround that worked for me was checking if the URL of the page contains the language code. Example for Spanish:
<script>
if(window.location.href.indexOf("/es/") > -1) {
alert("Spanish language");
}
</script>
You don't need DOM Ready for this, just place it on your header. Simple, right? :)

Related

including html tag in a php code, specifically dealing with href function inside a php block

enter the html code once a condition is satisfied but then i aim to use php values and html tags together.
tried tag etc and got most of the part in running just unable to deal with href beacuse it is referencing to some values.
not sure where to use "" or ``.
<?php.....
echo `Yes`;
?>
"yes" should work as a hyperlink but at the moment the php values are not processed that`s why no result.
You are using wrong sequence of quote
<?php
...
?>
<?php
echo '<a href="limitdatabase.php?Dropdown=' .
$_GET['Dropdown'].
';&search='.
$search_name .
';&wise='.
$_GET['wise'].
';">Yes</a>';
?>
and you should use single quote and not backtics for string
You are using incorrect concatenation. Whenever you want to add any PHP variable then you need to close the braces and need to start after PHP variable as below:
<?php
echo 'Yes';
?>
Also you should wrap Dropdown and wise in braces as it will not work directly in get as you have used. Hope it helps you!
You can use combination of html with php inside:
<a href="limitdatabase.php?Dropdown=<?PHP echo $_GET[Dropdown] . "&search="
. $search_name . "&wise=" . $_GET[wise]; ?>">Yes</a>
Also, you can input whole link in string:
<?php
$mylink = "limitdatabase.php?Dropdown=" . $_GET[Dropdown] . "&search=" . $search_name . "&wise=" . $_GET[wise];
?>
YES

Passing php variable to button onclick function

I am a newbie working on a screen which has a conditional button which should appear only if $step==2. In this case, the button is shown and on click, redirects me to another page within my project. However, the url to which it should redirect me is not fixed, but has a parameter within it. I am having trouble getting the onclick to work!
I have tried defining a $path variable in which I store the relocation string. I do this in the following way:
<?php $path = '"window.location=\'{{ url("/location/'.$location->id.'/description") }}\'"'?>
When I echo this $path, I get exactly the string I need, which is:
"window.location='{{ url("/location/2/description") }}'"
(Note: The $location->id in my test case is 2)
However, I then try using this $path for the onclick of the button and it's not working! Here is the whole thing:
<?php if ($step == 2) : ?>
<?php $path = '"window.location=\'{{ url("/location/'.$location->id.'/description") }}\'"'?>
<button class="button_s_red" onclick=<?php echo $path; ?>>{{ trans('setup_process.STEP2_Button') }}</button>
<?php endif; ?>
When I inspect this button in chrome, it seems that on the onclick, the / character is not getting through. But this is odd since when I echo the $path I do get this character, and / doesn't seem to be an escape character from what I've googled.
I've tried pretty much everything and googled every possible help but can't seem to make it work! Thanks in advance for your help!
id.'/description") }}\'"'?> >{{
trans('setup_process.STEP2_Button') }}
I think u must use " in Your code like this.
<?php if ($step == 2) : ?>
<?php $path = '"window.location=\'{{ url("/location/'.$location->id.'/description") }}\'"'?>
<button class="button_s_red" onclick=("<?php echo $path; ?>")>{{ trans('setup_process.STEP2_Button') }}</button>
<?php endif; ?>

Writing HTML code in Javascript inside a PHP code

I need to write in PHP, inside a Javascript portion code, an hidden input tag with an javascript array that I need to pass to another PHP code ..
This is the sample code ...
echo '<script type="text/javascript">';
<other javascript code .... >
echo 'arr_selections_json = JSON.stringify(arr_selections);';
echo 'document.write("<input type="hidden" name="arr_selections_json" value="+arr_selections_json+" />")';
This code doesn't work .... Any suggestions?
Thank you in advance .. .
You need to JS-escape the double quotes inside the argument of document.write()
e.g.
echo 'document.write("<input type=\\"hidden\\" name=\\"arr_selections_json" value=\\"" + arr_selections_json + "\\" />")';
Also, I've had recent weird cases, where document.write wouldn't consider the closing slash (/>), whereas it's HTML-5 compliant. I had to escape with a backslash the closing slash.
What about this ?
<script type="text/javascript">;
<other javascript code .... >
arr_selections_json = <?php echo JSON.stringify(arr_selections); ?>;
document.write('<input type="hidden" name="+arr_selections_json+" value="+arr_selections_json+" />');
</script>
I'm not sure what are your php part, but i think you should be ok like this.

Why yii captcha always shows a fixed picture?

Captcha works with no problem, but I have no idea why it's not generating a new code to dislay? I've looked into the documents but could find something that could solve my problem.
Is there something here that I'm missing. thanks.
public function actions()
{
return array(
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF,
),
);
}
my view file:
<?php echo $form->labelEx($model,'verifyCode'); ?>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model,'verifyCode'); ?>
<?php echo $form->error($model,'verifyCode'); ?>
This is a known bug, that would most likely be fixed in Yii2.
On the Yii forums, user Black suggests:
My solution was to remove the session key on my controller action on get. Be careful not to remove it in any other place because it will probably fail on server validation.
$session = Yii::app()->session;
$prefixLen = strlen(CCaptchaAction::SESSION_VAR_PREFIX);
foreach($session->keys as $key)
{
if(strncmp(CCaptchaAction::SESSION_VAR_PREFIX, $key, $prefixLen) == 0)
$session->remove($key);
}
Another way to workaround would be to use JavaScript to click on the refresh link on every page load as mentioned by Soph:
$(function() {
$('#yw0_button').click();
});

Passing a SESSION variable to JS

Okay, here's something that may seem rather trivial, but it's been giving me a headache, and I must have some sort of mental block now.
I want to pass a SESSION variable into JS, and then ultimately onto another page... It's only the first part that I'm worried about for now, because I can't even get a good "alert". The variable is blank... "the page at xxxxx says ____", even though the username shows up in the HTML span tag.
So, here's the applicable code on my page.
EDIT - For those asking why I would do this, I read it on (this) post...
EDIT: Solved. The method to do this is best done with:
var player = "<?php echo $_SESSION['username']; ?>";
Didn't need jQuery or other code. It is simple and direct, and shame on me for just sticking with one proposed solution from the other post.
In answer to some other questions: I did already have start session() at the top of the file. The script was after the HTML/PHP code. ***
... HTML stuff...
You are <span id='username'><?PHP echo $_SESSION['username']?></span></div>
...more HTML...
<script>
function rollpublic()
{
... some declarations...
var player = document.getElementById("username").innerHTML;
alert (player);
more JS }
</script>
Edit
Sidenote: One reason which may be the cause of your session name not appearing, may be because session_start(); is not present in your pages, nor is there any mention of it in your posted code.
Without seeing FULL code, is hard to pinpoint it.
However, here is a successful test that I performed using the following codes: (page 1 & page 2)
Page 1:
<?php
session_start();
$_SESSION['username'] = "USERNAME";
?>
Check session name
Page 2: (sessions_check.php) which will echo and alert the session name.
<?php
session_start();
?>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function(){
var player = "<?php echo $_SESSION['username']; ?>";
alert (player);
});
</script>
You are <span id='username'><?PHP echo $_SESSION['username']?></span></div>
Appearing in HTML source: var player = "USERNAME";
and You are <span id='username'>USERNAME</span></div>
N.B.: session_start(); needs to be inside all of the pages using sessions in order for this to work properly.
(Original answer) This worked for me:
<?php
session_start();
$_SESSION['username'] = "USERNAME";
?>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function(){
var player = "<?php echo $_SESSION['username']; ?>";
alert (player);
});
</script>
You are <span id='username'><?PHP echo $_SESSION['username']?></span></div>
You may call session by ajax:
function getSession(){
var session;
$.get('someUrlThatReturnSession', function(sessionData) {
session = sessionData;
});
return session;
}
You can assign SESSION variable to javascrip variable..
<script>
function myFunction(){
var a = <?php echo $_SESSION['var'] ?>;
alert(a);
}
</script>
If you need more help let me know..
If you're using Angular JS, you can use this approach. (Provide the PHP var to the ng-init param of a hidden input)

Categories

Resources