Why is this not working?
<?php
mysqli_select_db($connect,"dev");
$response = "Select response from revbut where session='$u'";
$rquery = mysqli_query($connect,$response);
$responseanswer = mysqli_fetch_array($rquery);
$re = $responseanswer['response'];
?>
<script type="text/javascript">
<?php echo $re; ?>
</script>
$re inside JavaScript is not getting echoed. But if I place it inside the above PHP function, it is getting echoed.
EDIT – BUT THEN WHY IS THIS NOT WORKING?
if(<?php echo $re; ?>){
document.getElementById('hide').style.display = "none";
}
if I PLACE the hide function outside the if – it is working.
It get’s echoed, but you won’t see anything on your page as the text will be written within the Javascript-tag which is not displayed by the browser. Look at your page source to verify that the text is really there.
EDIT
Try
This will ensure that your PHP string will be converted into the appropriate Javascript type – in case of strings it’ll ensure that the string is enclosed in
"and is escaped properly.EDIT again
When you do the following
this is what is written to the HTML page (that’s then interpreted by the browser)
But this is not even valid Javascript. What you want is
Note the
"which ensures that the whole thing is valid Javascript and that the contents of$rewill be interpreted as an Javascript string by the Browser’s Javascript engine. The call tojson_encode()does exactly this – it transforms PHP variables into the appropriate Javascript variables.