I need to run a PHP file from my AJAX Jquery.. Here is my try...
My Button as below
<input type="button"
style="background-color:#00BFFF;padding:3px;border: none;"
id="btnnew2"
class="button"
name="pc73"
value="Get The Latest"
/>
My Jquery as below
$(document).ready(function(){
$('.button').click(function(){
if(!confirm('Are you sure?')){
e.preventDefault();
return false;
}
else{
//$('#loading').show();
$.ajax({
url : 'testrun.php',
type : 'POST',
success : function (result) {
alert("done");
},
error : function () {
alert("error");
}
});
}
});
});
To have a better understanding I will attach testrun.php
as below,
<?php
$old_path = getcwd();
chdir('/home/scripts/');
$output = shell_exec("./testscript.sh");
chdir($old_path);
echo $output;
?>
and finally my testscript.sh as follows,
#!/bin/bash
cd /home/scripts/
export PATH="/root/anaconda3/envs/py36/bin:/usr/share/Modules/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
export TEMP=/home/svradmin/tmp
python3 testupdate.py
When I click the button I am getting an alert as done
but my testupdate.py
has not executed.. my testupdate.py as follows, from that I could identify whether the python script has run or not.
import pandas as pd
from fbprophet import Prophet
import altair as alt
import numpy as np
import statistics as st
import mysql.connector as sql
from sqlalchemy import create_engine
import pymysql
pymysql.install_as_MySQLdb()
alt.renderers.enable('notebook')
from pandas import DataFrame
from datetime import datetime
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]="3"
sql_transfer_Query = "insert into updatetest(text) values ('executed');"
print(sql_transfer_Query)
#Write to Final Alarm Table
db_connection_write = sql.connect(host='localhost', database='test', user='XXXX', password='XXX',charset='utf8')
cursor = db_connection_write.cursor()
cursor.execute(sql_transfer_Query)
db_connection_write.commit()
if (db_connection_write.is_connected()):
cursor.close()
db_connection_write.close()
print("MySQL connection is closed")
##exit()
I know this is too long process... But I need to stick on it due to some other requirement... Can someone help me to solve this issue?
Note: without button clicking, If I use bash testscript.sh
to run the script. I could see my python file has run without any issue.. Hope this is clear..