doufei2328 2014-09-08 03:52
浏览 288
已采纳

单击按钮调用php函数

I am trying to call a php function on click on a button using Javascript. It does not seem to be working fine.

Is there a better way to call php function on click of a button

<!DOCTYPE html>
<html>

<head>
<script type="text/javascript">
function executeShellScript(clicked)
{
    var x="<?php ex(); ?>";
    alert(x);
    return false;
}
</script>
</head>

<body>


<input type="button" id="sample" value="click" onclick="executeShellScript()"/>

<?php

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

function ex(){

echo "Trying to run shell script from the web browser";
echo "<br>";

$contents = file_get_contents('/var/www/shellscriptphp/helloworld.sh');
echo shell_exec($contents);

$result = shell_exec('sh /var/www/shellscriptphp/helloworld.sh');
echo $result;
}

?>

</body>
</html>
  • 写回答

1条回答 默认 最新

  • dongpang2483 2014-09-08 03:58
    关注

    You cannot invoke a php function just like the way you have explained above. Because php script executions happen before the source of the webpage is sent to the client browser from the server.

    However you can do it via an ajax call in which you invoke a client side side js function onclick of the button and and that function inturn makes an ajax call to a server side page and returns the result back.

    example:

    Here is a sample code which you may refer. This page makes a POST ajax request to itself and gets the response back. Let me know incase of errors as i havent run it here.

    <?php
    /** this code handles the post ajax request**/
    if(isset($_POST['getAjax'])) {
    
        /* you can do this below settings via your php ini also. no relation with our stuff */
        ini_set('display_errors',1);
        ini_set('display_startup_errors',1);
        error_reporting(-1);
    
        /* setting content type as json */
        header('Content-Type: application/json');
        $result = shell_exec('sh /var/www/shellscriptphp/helloworld.sh');       
        /* making json string with the result from shell script */
        echo json_encode(array("result"=>$result));
        /* and we are done and exit */
        exit();
    }
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://code.jquery.com/jquery-2.1.1.js" type="text/javascript"></script>
    <script type="text/javascript">
    function executeShellScript(clicked)
    {
        //$_SERVER["REQUEST_URI"] is used to refer to the current page as we have the ajax target as this same page
        $.post('<?PHP echo $_SERVER["REQUEST_URI"]; ?>',{"getAjax":true}, function(data) {
            alert(data['result']);
            return false;
        });
    
    
    }
    </script>
    </head>
    <body>
    <input type="button" id="sample" value="click" onclick="executeShellScript()"/>
    </body>
    </html> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存