I am trying to echo
a JQuery alert
from PHP. The method I am using is to echo the entire script tags and then the JQuery within it. JQuery is working because alerts elsewhere work. the JQuery library is loaded before the code i am trying so it is not that.
below is my current statement:
date_default_timezone_set('Europe/London'); #server timezone
$now = date('His'); #current time
$cl = '173000'; #closing time
$op = '084500'; #opening time
switch (true) {
case $now < $cl && $now > $op:
echo "open
<script type='text/javascript'>
$(document).ready(function(){
alert('open');
});
</script>
";
break;
case $now > $cl && $now < $op:
echo 'closed';
break;
default:
echo("neither closed nor open. dun dun duuuuun");
break;
}
I know the statement is working to some extent because the open string is displayed. I also see the script tags yet they don't seem to execute see below:
This image shows that the open text just before the script is echo'd into the page
This image shows that the script tag is put into the page but is not executed
The answer was that JQuery hadn't actually loaded like i thought it had. the version number was wrong. for some reason it was:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.4.3/jquery.min.js"></script>
I have now changed it to:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2/jquery.min.js"></script>
the reason I left the 2.x.x out is because the hosted library will choose the most up to date version of 2 now.
Are you sure you have jQuery loaded at that moment you want to alert? By the way, If you want to just alert
something, you don't really need jQuery, just use:
<script>alert('open');</script>
This can be a few issues.
is the jQuery library actually included on the page? also check the console logs for any errors. any javascript error on the page will prevent any other javascript from executing.