<?php if(isset($_SESSION['logged_in'])) {
$somevar= "something";
?>
<script type="text/javascript">
var somevar2 = <?php echo $value; ?>;
</script>
<?php } ?>
I put the code inside <head>, the console shows somevar2 is undefined, I was expecting the script don't run because it put in a scope where it execute only when user is logged in. What is the cause of this problem?
<?php if(isset($_SESSION['logged_in'])) {
$somevar= "something";
?>
<script type="text/javascript">
var somevar2 = <?php echo $somevar; ?>;
</script>
<?php } ?>
That should do the job. My guess is that you just forgot to change the variable echoed.
Related
var secret_photo
var secret_name
if (!<?php echo isset($_SESSION['fb_item'])?'true':'false'; ?>) {
if($.cookie("domain[user]")){
secret_photo = 'noavatar.png';
secret_name = 'admin';
}
} else {
secret_photo = <?php echo json_encode($_SESSION['fb_item']['url']); ?>;
secret_name = <?php echo json_encode($_SESSION['fb_item']['name']); ?>;
}
I need to set php session to html using JavaScript .There is 2 logins , one is the facebook and the other is my own login system, facebook uses session and my own login sets cookie.
When I login by facebook , things are good , but if I login using my own login system , that echo check is going to cause problem , so how am I going to fix it ? Check if the session is available or not even if it doesn't exist it wont cause any problem.
You are probably encounter an error at the bottom of your code. You can't access $_SESSION['fb_item'] if it doesn't exist, it either shows a Notice or generate wrong js code secret_photo = ;. Just move condition to php side:
var secret_photo
var secret_name
<?php if(!isset($_SESSION['fb_item'])):?>
if($.cookie("domain[user]")){
secret_photo = 'noavatar.png';
secret_name = 'admin';
}
<?php else:?>
secret_photo = <?php echo json_encode($_SESSION['fb_item']['url']); ?>;
secret_name = <?php echo json_encode($_SESSION['fb_item']['name']); ?>;
<?php endif;?>
<script type="text/javascript">
var secret_photo
var secret_name
<?php if(!isset($_SESSION['fb_item'])){?>
if($.cookie('user')){
secret_photo = 'noavatar.png';
secret_name = 'admin';
}
<?php }else{?>
secret_photo = <?php echo json_encode($_SESSION['fb_item']['url']); ?>;
secret_name = <?php echo json_encode($_SESSION['fb_item']['name']); ?>;
<?php }?>
</script>
I'm having a problem trying to use a PHP variable within JavaScript. I keep getting the following message.
"Invalid or unexpected token. Message: Undefined variable: example."
I'm unsure why example is being undefined, as it is defined within the php code. Here is my code:
<?php
$example = '2';
?>
<script type="text/javascript">
var php_var = "<?php echo json_encode($example); ?>";
</script>
Does anyone have any suggestions? I have also tried the following javascript that results in the same problem:
<script type="text/javascript">
var php_var = "<?php echo $example; ?>";
</script>
Firstly, your original code has a syntax error: $example = '2' needs a semicolon.
Secondly, the next piece of code is just assigning the string <?php echo $example; ?> to the JavaScript variable php_var where the $example PHP variable is first substituted. The $example variable should be initiated properly first, however, for this to work.
As a separate note: JS cannot execute PHP directly -- only a PHP server can do so. What you're most likely trying to do is this:
<?php
$example = '2';
?>
<script type="text/javascript">
var php_var = '<?php echo $example ;?>';
</script>
This should work, use single quotes
<?php
$example = '2';
?>
<script type="text/javascript">
var php_var = '<?php echo $example; ?>';
</script>
Tried and working both variant:
<?php
$example = '2';
?>
<script type="text/javascript">
var php_var = <?php echo json_encode($example); ?>;
console.log(php_var);
</script>
<script type="text/javascript">
var php_va = "<?php echo $example; ?>";
console.log(php_va);
</script>
How I can set the JavaScript variable strUser from PHP?
I am using the following code:
<script>
function val()
{
var e = document.getElementById("ali");
var strUser = e.options[e.selectedIndex].text;
}
</script>
brand<select id="ali" onChange="val()">
<?php
$brand=modsearchkhodroHelper::retrieve();
foreach($brand as $item)
{
?>
<option value="<?php echo $item['brand']?>" selected="<?php $id=$item['brand']?>">
<?php echo $item['brand']?>
</option>
<?php
}
echo "</select>";
?>
If you want to set the variable when the page loads, you could use something like this in the PHP code:
<script type="text/javascript">var strUser = <?php echo json_encode($someVariable); ?>;</script>
Just make sure to remove the later variable declaration from the JavaScript.
If you want to set the variable after the page loads, you'll have to use an AJAX call to ge the value from the server.
Use Cookie
in your javascript
<script type="text/javascript">
document.cookie = "cookieName=cookieValue";
</script>
in your php
<?php
$phpVar = $_COOKIE['cookieName'];
echo $phpVar;
?>
How can I access PHP variables from Javascript where the PHP code exists in a separate file?
The code I have is:
<?php
$res = "OK";
?>
<html>
<head>
<script type="text/javascript">
function myFunc()
{
res = <?php echo json_encode($res); ?>;
console.log("res = " + res);
}
</script>
</head>
<body>
<button onclick="myFunc()">Click</button>
</body>
</html>
Now, if I were to move my php code to a separate file called a.php, how can I access $res? Assume I am calling a.php with a GET request (XMLHttpRequest object).
Try-
res = "<?php echo json_encode($res); ?>";
How can I echo this javascript if the php error messages is called? I have an error message setting that when a user misses his username or password it triggers an error message. The php error message is called by a php code. Here is the code:
<?php echo showmessage($msg) ?>
I have an alert message in javascript that when called it will make a javascript css pop up alert box. IF the javascript code is present it will show the alert box right away after reload. Here is code:
<script type="text/javascript">
$(document).ready(function () {
jqxAlert.alert('Alert Message');
})
</script>
How can I incorporate so that when the php echo message comes up it will trigger the javscript alert message. I was trying an if in php, so something like this code:
if ( showmessage($msg) ) {
<script type="text/javascript">
$(document).ready(function () {
jqxAlert.alert('Alert Message');
})
</script>
}
How can I echo my javascript message on the php call?
This could be written like this:
endif
endif-stackoverflow
<?php if (showmessage($msg)): ?>
<script>
$(document).ready(function (){
jqxAlert.alert('Alert Message');
});
</script>
<?php endif; ?>
Or like this:
<?php if (showmessage($msg)) { ?>
<script>
$(document).ready(function (){
jqxAlert.alert('Alert Message');
});
</script>
<?php } ?>
Or like this:
<?php
if (showmessage($msg)) {
echo
'
<script>
$(document).ready(function (){
jqxAlert.alert(\'Alert Message\');
});
</script>
';
}
?>
short tags
short hand comparison (ternary)
ternary 2
Or even this (probably not the best idea though):
<?= (showmessage($msg)) ? '<script>$(document).ready(function (){jqxAlert.alert(\'Alert Message\');});</script>' : ""; ?>
Alternatively, if you mean you would like to put the error message in that alertbox
<?php
if (showmessage($msg)) {
echo
'
<script>
$(document).ready(function (){
jqxAlert.alert('.showmessage($msg).');
});
</script>
';
}
?>
and again in the first style:
<?php if (showmessage($msg)): ?>
<script>
$(document).ready(function (){
jqxAlert.alert(<?= showmessage($msg) ?>);
});
</script>
<?php endif; ?>
something like this.. close your php tag before starting to write javascript..
<?php if ( showmessage($msg) ) { ?>
<script type="text/javascript">
alert('Alert Message');
</script>
<?php } ?>
Can you try this
<?PHP if ( showmessage($msg) ) { ?>
<script type="text/javascript">
$(document).ready(function () {
jqxAlert.alert('Alert Message');
});
</script>
<?PHP } ?>