Consider this simple example;
<?php $text = test; ?>
<script type="text/javascript" defer="defer">
var test;
test = "<?php echo $text; ?>"
$(document).ready(function(){
alert(test);
});
</script>
This works fine. Creating the alert with the text from the php var. However, if I place;
<?php $text = test; ?>
below the script – it does not work. I’ve tried the defer function. What am I doing wrong?
Cheers
If you place
below the JS code, the the variable $text is not defined yet, so you cannot echo it earlier (edit) in the script.