简体   繁体   English

单击按钮调用php函数

[英]Call php function on click of a button

I am trying to call a php function on click on a button using Javascript. 我试图通过使用Javascript点击按钮来调用php函数。 It does not seem to be working fine. 它似乎没有正常工作。

Is there a better way to call php function on click of a button 有没有更好的方法来点击按钮调用PHP功能

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

You cannot invoke a php function just like the way you have explained above. 你不能像上面解释的那样调用php函数。 Because php script executions happen before the source of the webpage is sent to the client browser from the server. 因为php脚本执行发生在网页源从服务器发送到客户端浏览器之前。

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. 但是你可以通过一个ajax调用来实现它,你可以在其中调用一个客户端js函数onclick按钮,并且该函数inturn对服务器端页面进行ajax调用并返回结果。

example: 例:

Here is a sample code which you may refer. 以下是您可以参考的示例代码。 This page makes a POST ajax request to itself and gets the response back. 此页面向自己发出POST ajax请求并获取响应。 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM