0

I know it is not possible to simply call a function through ajax. As it only sends data via HTTP headers. However, I am trying to achieve something. I'm trying to execute a piece of PHP code, by the click of a button. The code consists of a shell_exec("omxplayer file.mp3") So my ultimate goal is, to have a page load, display a button, which in turn, once clicked, will execute this piece of code (shell command).

I have looked for solutions and have not found one, even though there have been a lot of questions asked with a similar title to mine.

How can I achieve this concept?

EDIT: My ultimate goal is to use the shell_exec() to start playing a file using omxplayer on a linux machine.

1
  • There are many tutorials online. Commented May 1, 2015 at 21:28

3 Answers 3

3

You can use .get()

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
    $("button").click(function() {
        $.get("script.php?code=myFunction", function(data, status) {
            alert("Data: " + data + "\nStatus: " + status);
        });
    });
});
</script>
</head>
<body>
    <button>Execute Command</button>
</body>
</html>

script.php

if (!empty($_GET['code']) {
    $output = shell_exec(<do shell command here>):
    echo $output;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Please define "doesn't work", also, add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything.
2

The following assumes that xxx is your PHP file and output is the response from the xxx file:

button.onclick = function() {
    $.ajax({
        url: '/my/site/xxx.php',
        data: {action: 'test'},
        type: 'post',
        success: function(output) {
            alert(output);
        }
    });
};

1 Comment

I'm not looking for a direct copy of a question which I've already seen.
0

You are looking at things the wrong way.

Create a separate page that executes the exec command and displays the result.

Create an interface page where - and you have a wide choice of options - when the user clicks a button you send him to the exec page.

I'm assuming you need or want to feed data to the exec command. The page with the exec command can process values sent using GET and more securely POST (or REQUEST) Just like any other html form would.

If you want to create a somewhat modern feel to an old school browser bash implementation you have a few options.

  1. Submit onto self. Every time you submit the page will be redrawn with the new data and the new exec will have run.

  2. Submit into a target iframe. The interface will linger, the iframe will update the contents.

  3. xhtmlhttp post (asynchronous or "ajax" even though it doesn't have to be) and update the contents of a div with the response - from a separate php page.

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.