1

In my homeserver management application I added a function to wake PC's remotely via the application. The PHP-script that does this works perfectly, but I want to be able to do this via an AJAX-call.

The call happens like this:

User clicks a PHP-generated link:

<a href=\"javascript:wake('$hostname')\">Wake</a>

Where the function (jQuery) is:

function wake(hostname) {
    $(document).ready(function()
    {
       alert('function works');
       $.post('ajax/wake.php',{host: hostname});
       alert('Command executed');
    });
}

Both alerts are shown, which means the AJAX-call is executed. The php-script looks like this:

<?php
include_once("../classes/BLClient.php");
$blClient = new BLClient(true);
$hostname = $_GET["host"];

$client = $blClient->getClientByHostname($hostname);
$mac = $client->getMac();

echo `sudo etherwake -i eth1 $mac`;
?>

However, my PC's are not woken. If I browse directly to the script, it does work...

Jeroen
  • 267
  • 6
  • 20

3 Answers3

1

You're firing a $.post ajax request, but you're reading a $_GET parameter in your PHP script. Do a $.get ajax request instead, otherwise the data will be in $_POST not in $_GET.

Alternatively you can make use of the $_REQUEST superglobal which contains both post and get variables in PHP.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • DOH! That means I've been looking over that for about 3 months now, silly me... It works, thanks! All other answers are correct as well ofcourse. – Jeroen Jul 06 '11 at 08:46
0

you might need to use

$_POST["host"] or $_REQUEST["host"] in your php script, as you are doing a Jquery post.

In the browser, you might be passing host like :

 http://www.mysite.com/ajax/wake.php?host=xxxxxxxx

Which results in "host" being present in $_GET .

DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
0

Yor are making post call '$.post' ,so here you have to use "$_POST["host"]"

mymotherland
  • 7,968
  • 14
  • 65
  • 122