Asim Zaidi
Asim Zaidi

Reputation: 28284

alert box empty for php javascript

why does this script show empty alert box. I am trying to use php value in javascript

<script type="text/javascript">
 alert(<?php echo count($myorder) ?>); </script> 

Upvotes: 0

Views: 1304

Answers (2)

Pekka
Pekka

Reputation: 449435

Probably because you are not encapsulating the PHP output into quotes (JavaScript should give you an error on that?), and the echo statement is missing a mandatory optional semicolon.

Try

<script type="text/javascript">
 alert("<?php echo count($myorder); ?>"); </script> 

Upvotes: 5

David Larrabee
David Larrabee

Reputation: 384

remove the javascript - and just echo the variable to the screen, what does the value show?

looking at the sample, even though as the previous answer mentioned you missed the ; at the end of the echo, and you didn't quote the alert, it still actually works at least in firefox and IE8. The count should always return a value even on a null value or a non-array, so not 100% sure but would be interested to see what it shows in php only.

just for sanity sake try this....

<?php echo "before | " . count($myorder) . " | after";?>

and what is the output....

if the value is not an array, is null, or is an array with zero entries it should be

before 0 after

and if it has elements it would be whatever the count is, obviously.

Upvotes: 2

Related Questions