Skip to content
Advertisement

jQuery code isn’t working but also shows no errors

I have some jQuery code used to call a separate PHP file (which also contains a jQuery countdown clock) and none of the jQuery sections of code seem to load, despite there being no errors showing at all.

The code I am using to call the file stats_bar.php is:

JavaScript

and stats_bar.php contains the following code:

JavaScript

When using include('ajax/stats_bar.php'); instead of the jQuery code to call the PHP file, it all works exactly as expected, with the exception of the jQuery countdown clock within the PHP file which is behaving in the same way as the jQuery code used to call the PHP file, however I have also checked the countdown clock code for errors and it also doesn’t show any so I am now stumped?

Is there something I am missing here or have overlooked?

UPDATE

The correct answer given by selfagency can be used to achieve the desired outcome by using GET and POST when the content is in a separate PHP file, however I was also able to use the information provided in the correct answer to produce the same effects using just the main page which meant no delay between loading the content – I have included the code below.

PLEASE NOTE, what has been achieved here may not be the best outcome overall as what I have found with both approaches is that because the page is constantly refreshing sections of PHP and therefore continually requesting data, the number of requests and overall bandwidth is astronomical, to the point where pages that already contained even a small number of PHP requests will quickly crash.

I will continue to look for a better solution!

JavaScript

Advertisement

Answer

The jQuery load method will not execute JavaScript contained in an HTML fragment. Try using the get method instead and using the callback to append the fragment to your page.

Try:

JavaScript
JavaScript

Notice the change from:

JavaScript

to <script>.

You need to put <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> in your document <head> and just use <script> at the beginning of a code block.

You also seem to have some errors with your JavaScript in the PHP file you’re loading. Replace this:

JavaScript

with just <script>.

Then at the bottom, change:

JavaScript

to:

JavaScript

POST method example:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement