Zaw Myo Htet
Zaw Myo Htet

Reputation: 540

Get php boolean value with javascript

I want to get php booelan value with javascript. That is my code.

<script type="text/javascript">

var $j = jQuery.noConflict();

var isValue = $j(<?php isset($getResult[]); ?>);

alert(isValue);

</script>

Upvotes: 0

Views: 2711

Answers (3)

Sabari
Sabari

Reputation: 6335

It should be like this

<script type="text/javascript">

    var $j = jQuery.noConflict();

    var isValue = $j(<?php if (isset($getResult)){ echo true;} else {echo false;} ?>);

    alert(isValue);

</script>

You cannot use $getResult[] for reading using issest. It will return fatal error or you have to specify some index in the array.

If you want to check if it is an array then you can use :

<script type="text/javascript">

    var $j = jQuery.noConflict();

    var isValue = $j(<?php if (isset($getResult) && is_array($getResult)){ echo true;} else {echo false;} ?>);

    alert(isValue);

</script>

Upvotes: 0

Bobby Stenly
Bobby Stenly

Reputation: 1290

php is a server side programming, and javascript is client side. so you need to "echo" the php value to get result on javascript

var isValue = $j(<?php echo isset($getResult[]) ? "true" : "false"; ?>);

Upvotes: 3

hek2mgl
hek2mgl

Reputation: 157947

You'll have to convert the boolean result of isset() into a string representation of 'true' or 'false' so that it can be understood by the javascript parser. Like this:

var isValue = $j(<?php isset($getResult[]) ? echo 'true' : echo 'false'; ?>);

Upvotes: 1

Related Questions