I have to run JavaScript using PHP variables the code have PHP if statement and if true javascript will execute but the script using a second PHP variable.
<?php
if ($time_close_option == 1){
echo '<script type="text/javascript">';
// close the div in $timeoutdiv secs
echo 'window.setTimeout("closePromoSpace();",' <?php echo $timeoutdiv;?>);';
echo 'function closePromoSpace()
{
document.getElementById("promospace").style.display=" none";
};';
echo'</script>';
}
?>
It looks like you're using $timeoutdiv
as a string, not a number. Try
<?php if ($time_close_option == 1) { ?>
<script type="text/javascript">
// close the div in $timeoutdiv secs
window.setTimeout(closePromoSpace(), <?php echo $timeoutdiv;?>);
function closePromoSpace()
{
document.getElementById("promospace").style.display=" none";
};
</script>
<?php } ?>
Notice that we can clean the code up a lot by not using the echo
statements, but by switching between php and html contexts.
<?php if ($time_close_option == 1)
{
echo '<script type="text/javascript">';
echo 'function closePromoSpace()
{
document.getElementById("promospace").style.display="none";
}';
echo "window.setTimeout(closePromoSpace, $timeoutdiv);";
echo '</script>';
}
?>
<script type="text/javascript">
<?php
if ($time_close_option == 1)
{?>
window.setTimeout(closePromoSpace, '<?php echo $timeoutdiv; ?>');
function closePromoSpace()
{
document.getElementById("promospace").style.display = "none";
}
<?php
} ?>
</script>
Try this one