Checking if 2 php variables are set inside Javascript isset
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have 2 php variables in PHP (mainly $usm and $ag) and am passing them to the frontend. In Javascript am using isset to check if they have a value before executing some code but seems not to work
<script>
if( <?php isset($usm , $ag) ?> ){
$( document ).ready(function() {
var usmData = {!! json_encode($usm) !!};
var agData = {!! json_encode($ag) !!};
});
}
</script
javascript php variables
add a comment |
I have 2 php variables in PHP (mainly $usm and $ag) and am passing them to the frontend. In Javascript am using isset to check if they have a value before executing some code but seems not to work
<script>
if( <?php isset($usm , $ag) ?> ){
$( document ).ready(function() {
var usmData = {!! json_encode($usm) !!};
var agData = {!! json_encode($ag) !!};
});
}
</script
javascript php variables
You may not access the PHP variable within the script. Try to set the php variable in cookies or sessions and extract the php variable data from there.
– Ramya
Jan 3 at 9:55
add a comment |
I have 2 php variables in PHP (mainly $usm and $ag) and am passing them to the frontend. In Javascript am using isset to check if they have a value before executing some code but seems not to work
<script>
if( <?php isset($usm , $ag) ?> ){
$( document ).ready(function() {
var usmData = {!! json_encode($usm) !!};
var agData = {!! json_encode($ag) !!};
});
}
</script
javascript php variables
I have 2 php variables in PHP (mainly $usm and $ag) and am passing them to the frontend. In Javascript am using isset to check if they have a value before executing some code but seems not to work
<script>
if( <?php isset($usm , $ag) ?> ){
$( document ).ready(function() {
var usmData = {!! json_encode($usm) !!};
var agData = {!! json_encode($ag) !!};
});
}
</script
javascript php variables
javascript php variables
asked Jan 3 at 9:52
MartinMartin
1841111
1841111
You may not access the PHP variable within the script. Try to set the php variable in cookies or sessions and extract the php variable data from there.
– Ramya
Jan 3 at 9:55
add a comment |
You may not access the PHP variable within the script. Try to set the php variable in cookies or sessions and extract the php variable data from there.
– Ramya
Jan 3 at 9:55
You may not access the PHP variable within the script. Try to set the php variable in cookies or sessions and extract the php variable data from there.
– Ramya
Jan 3 at 9:55
You may not access the PHP variable within the script. Try to set the php variable in cookies or sessions and extract the php variable data from there.
– Ramya
Jan 3 at 9:55
add a comment |
4 Answers
4
active
oldest
votes
You need to check the isset part with PHP only.
<?php if(isset($usm) and isset($ag)){ ?>
<script>
$( document ).ready(function() {
var usmData = '<?php echo json_encode($usm); ?>';
var agData = '<?php echo json_encode($ag); ?>';
});
</script>
<?php } ?>
JS strings need to be surrounded by'
or"
– Vahid Amiri
Jan 3 at 10:05
Yes fixed also his encoding in JSON
– executable
Jan 3 at 10:06
@executable I tried your solution and I get this error Parse error: syntax error, unexpected '{'
– Martin
Jan 3 at 10:08
Can you show a sample data of$usm
and$ag
– executable
Jan 3 at 10:08
Noted the error,, it works fine thanks alot..
– Martin
Jan 3 at 10:13
add a comment |
That doesn't make sense. You mixed up PHP and JS.
Result of PHP isset
is only used on PHP and doesn't make it to JS side. Your JS if statement is not valid.
Check this article for more info.
But I placed it inside php tags <?php ... ?>
– Martin
Jan 3 at 9:58
1
@Martin It doesn't matter because Javascript expects a boolean value insideif
and your the result of your PHP code doesn't get passed to JS. not like that anyway.
– TuckerS
Jan 3 at 10:01
add a comment |
<script>
<?php if(isset($usm , $ag)) { ?>
$( document ).ready(function() {
var usmData = {!! json_encode($usm) !!};
var agData = {!! json_encode($ag) !!};
});
<?php } ?>
</script>
add a comment |
I would suggest you to write the PHP code outside otherwise script
tag will be executed unnecessary with empty code if the condition is not met.
<?php
if(isset($usm , $ag) {
?>
<script>
$( document ).ready(function() {
var usmData = '<?php echo json_encode($usm); ?>';
var agData = '<?php echo json_encode($ag); ?>';
});
</script>
<?php
}
?>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019850%2fchecking-if-2-php-variables-are-set-inside-javascript-isset%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to check the isset part with PHP only.
<?php if(isset($usm) and isset($ag)){ ?>
<script>
$( document ).ready(function() {
var usmData = '<?php echo json_encode($usm); ?>';
var agData = '<?php echo json_encode($ag); ?>';
});
</script>
<?php } ?>
JS strings need to be surrounded by'
or"
– Vahid Amiri
Jan 3 at 10:05
Yes fixed also his encoding in JSON
– executable
Jan 3 at 10:06
@executable I tried your solution and I get this error Parse error: syntax error, unexpected '{'
– Martin
Jan 3 at 10:08
Can you show a sample data of$usm
and$ag
– executable
Jan 3 at 10:08
Noted the error,, it works fine thanks alot..
– Martin
Jan 3 at 10:13
add a comment |
You need to check the isset part with PHP only.
<?php if(isset($usm) and isset($ag)){ ?>
<script>
$( document ).ready(function() {
var usmData = '<?php echo json_encode($usm); ?>';
var agData = '<?php echo json_encode($ag); ?>';
});
</script>
<?php } ?>
JS strings need to be surrounded by'
or"
– Vahid Amiri
Jan 3 at 10:05
Yes fixed also his encoding in JSON
– executable
Jan 3 at 10:06
@executable I tried your solution and I get this error Parse error: syntax error, unexpected '{'
– Martin
Jan 3 at 10:08
Can you show a sample data of$usm
and$ag
– executable
Jan 3 at 10:08
Noted the error,, it works fine thanks alot..
– Martin
Jan 3 at 10:13
add a comment |
You need to check the isset part with PHP only.
<?php if(isset($usm) and isset($ag)){ ?>
<script>
$( document ).ready(function() {
var usmData = '<?php echo json_encode($usm); ?>';
var agData = '<?php echo json_encode($ag); ?>';
});
</script>
<?php } ?>
You need to check the isset part with PHP only.
<?php if(isset($usm) and isset($ag)){ ?>
<script>
$( document ).ready(function() {
var usmData = '<?php echo json_encode($usm); ?>';
var agData = '<?php echo json_encode($ag); ?>';
});
</script>
<?php } ?>
edited Jan 3 at 10:14
answered Jan 3 at 9:59


executableexecutable
1,9832924
1,9832924
JS strings need to be surrounded by'
or"
– Vahid Amiri
Jan 3 at 10:05
Yes fixed also his encoding in JSON
– executable
Jan 3 at 10:06
@executable I tried your solution and I get this error Parse error: syntax error, unexpected '{'
– Martin
Jan 3 at 10:08
Can you show a sample data of$usm
and$ag
– executable
Jan 3 at 10:08
Noted the error,, it works fine thanks alot..
– Martin
Jan 3 at 10:13
add a comment |
JS strings need to be surrounded by'
or"
– Vahid Amiri
Jan 3 at 10:05
Yes fixed also his encoding in JSON
– executable
Jan 3 at 10:06
@executable I tried your solution and I get this error Parse error: syntax error, unexpected '{'
– Martin
Jan 3 at 10:08
Can you show a sample data of$usm
and$ag
– executable
Jan 3 at 10:08
Noted the error,, it works fine thanks alot..
– Martin
Jan 3 at 10:13
JS strings need to be surrounded by
'
or "
– Vahid Amiri
Jan 3 at 10:05
JS strings need to be surrounded by
'
or "
– Vahid Amiri
Jan 3 at 10:05
Yes fixed also his encoding in JSON
– executable
Jan 3 at 10:06
Yes fixed also his encoding in JSON
– executable
Jan 3 at 10:06
@executable I tried your solution and I get this error Parse error: syntax error, unexpected '{'
– Martin
Jan 3 at 10:08
@executable I tried your solution and I get this error Parse error: syntax error, unexpected '{'
– Martin
Jan 3 at 10:08
Can you show a sample data of
$usm
and $ag
– executable
Jan 3 at 10:08
Can you show a sample data of
$usm
and $ag
– executable
Jan 3 at 10:08
Noted the error,, it works fine thanks alot..
– Martin
Jan 3 at 10:13
Noted the error,, it works fine thanks alot..
– Martin
Jan 3 at 10:13
add a comment |
That doesn't make sense. You mixed up PHP and JS.
Result of PHP isset
is only used on PHP and doesn't make it to JS side. Your JS if statement is not valid.
Check this article for more info.
But I placed it inside php tags <?php ... ?>
– Martin
Jan 3 at 9:58
1
@Martin It doesn't matter because Javascript expects a boolean value insideif
and your the result of your PHP code doesn't get passed to JS. not like that anyway.
– TuckerS
Jan 3 at 10:01
add a comment |
That doesn't make sense. You mixed up PHP and JS.
Result of PHP isset
is only used on PHP and doesn't make it to JS side. Your JS if statement is not valid.
Check this article for more info.
But I placed it inside php tags <?php ... ?>
– Martin
Jan 3 at 9:58
1
@Martin It doesn't matter because Javascript expects a boolean value insideif
and your the result of your PHP code doesn't get passed to JS. not like that anyway.
– TuckerS
Jan 3 at 10:01
add a comment |
That doesn't make sense. You mixed up PHP and JS.
Result of PHP isset
is only used on PHP and doesn't make it to JS side. Your JS if statement is not valid.
Check this article for more info.
That doesn't make sense. You mixed up PHP and JS.
Result of PHP isset
is only used on PHP and doesn't make it to JS side. Your JS if statement is not valid.
Check this article for more info.
edited Jan 3 at 11:12


MorganFreeFarm
1,48431328
1,48431328
answered Jan 3 at 9:57


TuckerSTuckerS
3313
3313
But I placed it inside php tags <?php ... ?>
– Martin
Jan 3 at 9:58
1
@Martin It doesn't matter because Javascript expects a boolean value insideif
and your the result of your PHP code doesn't get passed to JS. not like that anyway.
– TuckerS
Jan 3 at 10:01
add a comment |
But I placed it inside php tags <?php ... ?>
– Martin
Jan 3 at 9:58
1
@Martin It doesn't matter because Javascript expects a boolean value insideif
and your the result of your PHP code doesn't get passed to JS. not like that anyway.
– TuckerS
Jan 3 at 10:01
But I placed it inside php tags <?php ... ?>
– Martin
Jan 3 at 9:58
But I placed it inside php tags <?php ... ?>
– Martin
Jan 3 at 9:58
1
1
@Martin It doesn't matter because Javascript expects a boolean value inside
if
and your the result of your PHP code doesn't get passed to JS. not like that anyway.– TuckerS
Jan 3 at 10:01
@Martin It doesn't matter because Javascript expects a boolean value inside
if
and your the result of your PHP code doesn't get passed to JS. not like that anyway.– TuckerS
Jan 3 at 10:01
add a comment |
<script>
<?php if(isset($usm , $ag)) { ?>
$( document ).ready(function() {
var usmData = {!! json_encode($usm) !!};
var agData = {!! json_encode($ag) !!};
});
<?php } ?>
</script>
add a comment |
<script>
<?php if(isset($usm , $ag)) { ?>
$( document ).ready(function() {
var usmData = {!! json_encode($usm) !!};
var agData = {!! json_encode($ag) !!};
});
<?php } ?>
</script>
add a comment |
<script>
<?php if(isset($usm , $ag)) { ?>
$( document ).ready(function() {
var usmData = {!! json_encode($usm) !!};
var agData = {!! json_encode($ag) !!};
});
<?php } ?>
</script>
<script>
<?php if(isset($usm , $ag)) { ?>
$( document ).ready(function() {
var usmData = {!! json_encode($usm) !!};
var agData = {!! json_encode($ag) !!};
});
<?php } ?>
</script>
answered Jan 3 at 10:00
TDKTDK
961720
961720
add a comment |
add a comment |
I would suggest you to write the PHP code outside otherwise script
tag will be executed unnecessary with empty code if the condition is not met.
<?php
if(isset($usm , $ag) {
?>
<script>
$( document ).ready(function() {
var usmData = '<?php echo json_encode($usm); ?>';
var agData = '<?php echo json_encode($ag); ?>';
});
</script>
<?php
}
?>
add a comment |
I would suggest you to write the PHP code outside otherwise script
tag will be executed unnecessary with empty code if the condition is not met.
<?php
if(isset($usm , $ag) {
?>
<script>
$( document ).ready(function() {
var usmData = '<?php echo json_encode($usm); ?>';
var agData = '<?php echo json_encode($ag); ?>';
});
</script>
<?php
}
?>
add a comment |
I would suggest you to write the PHP code outside otherwise script
tag will be executed unnecessary with empty code if the condition is not met.
<?php
if(isset($usm , $ag) {
?>
<script>
$( document ).ready(function() {
var usmData = '<?php echo json_encode($usm); ?>';
var agData = '<?php echo json_encode($ag); ?>';
});
</script>
<?php
}
?>
I would suggest you to write the PHP code outside otherwise script
tag will be executed unnecessary with empty code if the condition is not met.
<?php
if(isset($usm , $ag) {
?>
<script>
$( document ).ready(function() {
var usmData = '<?php echo json_encode($usm); ?>';
var agData = '<?php echo json_encode($ag); ?>';
});
</script>
<?php
}
?>
answered Jan 3 at 10:11


Rajendra aroraRajendra arora
1,51211018
1,51211018
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019850%2fchecking-if-2-php-variables-are-set-inside-javascript-isset%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You may not access the PHP variable within the script. Try to set the php variable in cookies or sessions and extract the php variable data from there.
– Ramya
Jan 3 at 9:55