0

I need to let the code below to stop directly execute the code without button click. I want the function to run when button is click on it. Thanks for the help

<html>
<head>
<script language="javascript" type="text/JavaScript">
{
function popup() {
<?php echo shell_exec('sh bash_test.sh') ?>;
}
}    
</script>

</head>
<body>
<input type="button" onclick="popup()" value="popup">




</body>
</html>

4 Answers 4

2

The only way to do this is to make a form or change something in url like window.location and check vars. You cannot mix Javascript with PHP like that

script.php:

<html>
<head>
<script language="javascript" type="text/javascript">
    function popup(){
        window.location = 'script.php?run=shell';
    }
</script>
</head>
<body>
<input type="button" onclick="popup()" value="popup">
<?php
if(isset($_GET['run']) && ($_GET['run'] == 'shell')){
    echo shell_exec('sh bash_test.sh');
}
?>
</body>
</html>
Sign up to request clarification or add additional context in comments.

4 Comments

I understand that both cannot be mix together. but is there anyway to execute the shell file as i need to use php as well. and the sh file is not execute with the code provided. thanks for reply
I had a ! wrongly there, modified. And you should know you cannot execute a shell script if you don't have permissions.
i have dome all the permission stuff d. i try only php can be execute. but i need use button click so this is the problem i get when use javascript
window.location = 'script.php?run=shell';
0

your html is invalid. your </script> tag should be before the </head> tag

if you really want to execute it only when you press the button then you should consider using ajax or to use a <form>

1 Comment

sorry. copy and paste wrongly. but even put in correct place still will auto execute the script without me pressing the button
0

alert('<?php echo shell_exec('sh bash_test.sh') ; ?>');

Comments

0

You can do that using ajax-request, for example, with jquery:

<html>
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script language="javascript" type="text/JavaScript">
            {
                function popup() {
                    $.post('someScript.php');
                }
            }    
        </script>
    </head>

    <body>
        <input type="button" onclick="popup()" value="popup">
    </body>
</html>

In "someScript.php" you can put command

shell_exec('sh bash_test.sh');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.