4

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>
user3846091
  • 1,625
  • 6
  • 24
  • 29
  • 2
    since `jQuery` is tagged, use `$.ajax` – Kevin Sep 08 '14 at 03:54
  • Possible duplicate of: [How to Call a PHP Function on the Click of a Button](http://stackoverflow.com/questions/20738329/how-to-call-a-php-function-on-the-click-of-a-button). – JosEduSol Sep 08 '14 at 03:56

1 Answers1

3

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> 
Community
  • 1
  • 1
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101