0

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>
1
  • 1
    echo it? echo variable ? "true" : "false" ? Commented Apr 8, 2013 at 5:13

3 Answers 3

3

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"; ?>);
Sign up to request clarification or add additional context in comments.

Comments

1

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'; ?>);

1 Comment

hehe :) So the opener can be twice sure that this is the correct answer.
0

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>

1 Comment

this example is not working. you need to php echo in quotes. for example: <script> var isValue = <?php echo isset($getResult) ? 'true' : 'false'; ?> </script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.