0

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..

Fravadona
  • 13,917
  • 1
  • 23
  • 35
Codec737
  • 117
  • 6
  • What output do you get if you call `testrun.php` directly from the browser? – KIKO Software Jul 03 '22 at 13:45
  • The command is `bash testscript.sh` not just `./testscript.sh`? – KIKO Software Jul 03 '22 at 13:47
  • @KIKOSoftware when I directly run **php testrun.php** also I could see my python script has excecuted.. But this is not executing when I use ajax... – Codec737 Jul 03 '22 at 13:58
  • Have you checked that shell_exec is not disabled for that particular vhost? This would probably allow it to be run on cli but then fail when run from AJAX. – kissumisha Jul 03 '22 at 13:59
  • @kissumisha No I didn't check.. Kindly help me to check this... – Codec737 Jul 03 '22 at 14:00
  • @KIKOSoftware No I use **bash testscript.sh** to run directly... As mentioned previously both **bash testscript.sh** and **php testrun.php** works.. only issue with AJAX.. – Codec737 Jul 03 '22 at 14:06
  • 1
    `/root/anaconda3/envs/py36/bin`: I think, I HOPE, that your webserver user doesn't have access to `/root`. If all python dependencies are there then I would expect `python3 testupdate.py` to errput something like `ModuleNotFoundError: No module named 'pandas'` – Fravadona Jul 03 '22 at 14:23
  • @Fravadona does changing the root permission to 755 will resolve this issue? – Codec737 Jul 03 '22 at 14:47
  • @Codec737 https://stackoverflow.com/questions/21581560/php-how-to-know-if-server-allows-shell-exec – kissumisha Jul 03 '22 at 23:11
  • @Fravadona Same issue. I made whole path to 755 but still I could not run.. my php website is at var/www/html/project folder.. testrun.php also in same folder.. But when I directly run that php, my python is running. is there can be a permission issue then? – Codec737 Jul 04 '22 at 03:48
  • You should take a look at your http error logs; the issues should be reported there – Fravadona Jul 04 '22 at 05:48
  • 1
    FYI: `e.preventDefault();` - you have no `e` there. You need `$('.button').click(function(e){` to _receive_ the event object that gets passed to the callback function, so that it will be available under that name inside the function. – CBroe Jul 04 '22 at 07:29

0 Answers0