Click here to Skip to main content
15,793,139 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a PHP script that includes JavaScript code. When I call the PHP script manually on my browser, the JavaScript code executes successfully and performs the expected actions. However, when the same PHP script is executed via a cron job, the JavaScript code is printed as plain text instead of being executed. I am running on a Linux server (HOSTINGER). I run the same code on a Windows server and it works perfectly. 

<pre lang="PHP"><pre><?php

include "sqlserver.php";


<script src=""></script>

	async function trigger_test_function() {
		var output="Success";
		var temp_txid="a6s5df46asdf4";
		data: {trxid:output,temp_txid:temp_txid},
		success: function(response){
    } catch(error) {

What I have tried:

I am running the same code on a Windows server and it works perfectly. 
Updated 26-Jul-23 4:16am
Member 15627495 26-Jul-23 6:40am    
When Php codes are miswritten, the full contents display like a txt file.

I see in your file that your Php pattern stop before the javascript code.

to fix it, put the Js code in a var, then echo(in_a_var), or a bit more weighted : put echo(the line) before all the Js script.

you'll have to escape few chars " ' / to make reliable your javascript with your php.
Vikas Uniqu 26-Jul-23 6:46am    
"The same code that I have provided is running just fine on another server."
Richard Deeming 26-Jul-23 6:48am    
What do you mean by "executed via a cron job"? Are you loading a web browser, navigating to your page, and waiting for it to load and execute the JavaScript?

Why are you executing a PHP file to generate an HTML file to execute some JavaScript to make a network request, rather than writing code to make that web request directly?
Vikas Uniqu 26-Jul-23 8:05am    
I want this script to run every minute continuously. That is why I am using corn job. There are some lines of code I haven't added here. I have to make API calls and some calculations. To do that I will use this javascript function. Once these processes are completed the details will be submitted to this "update_trxid.php" file.

1 solution

If you run PHP as part of a cron then no Javascript will execute. PHP's function is to produce HTML content and return it to the browser, the browser then interprets the HTML (and Javascript) and executes whatever has been produced. The fact your PHP file is just returning the content as plaintext is correct, it's what the server is meant to do; you'd just normally have a browser receiving that content and executing the Javascript.

If you want your PHP file to run web requests when it triggers then you may want to checkout the cURL[^] documentation. Essentially you'll want any code to execute within PHP code, not in Javascript.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900