David
David

Reputation: 1296

Call PHP on server via Javascript?

I am attempting to make a dead simple webpage that does things when I click a button.

I am trying to use Javascript so I don't have to reload the main page or anything.

So I want a button that says "Shutdown", and I simply want to run shutDown.php that will shut down the server when I click on this.

I've been searching for an hour now and can't find a simple example. Could someone please post a very simple, example, or a link to one?

Many thanks :)

EDIT: Okay so here's my pitiful attempt.. it doesn't work.. any tips?

<html>
<head>
<title>Control Page</title>
</head>

<body>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript">
/*************************************
JQUERY EXAMPLE
*************************************/
$('#mysubmit').click(){
    var jqueryXHR = $.ajax({
        'type': 'POST',
        'url': 'http://localhost/killIE.php',
        'dataType': 'json'
       });
    }
}
</script>

<input type="submit" name="mysubmit" value="Click!" />


</body>
</html>

Upvotes: 4

Views: 4015

Answers (6)

ThatGuy
ThatGuy

Reputation: 15099

Yep - technology called called AJAX. You can either write it in raw javascript or use ready made libraries. The most popular JS library nowadays (subjectively) is jQuery, I'll use it in my example.

NOTE: this will trigger your shutDown.php PHP script on the server without reloading the page. But - you won't be able to parse web requests obviously after you shutdown your server :)...

/*************************************
JQUERY EXAMPLE
*************************************/
$('#myShutdownButton').click(){
    var jqueryXHR = $.ajax({
        'type': 'GET',
        'url': 'http://someServer.com/shutDown.php',
        'data': {'shutdownServer': 'yes'},//optional
        'dataType': 'json'
       });
    }
}

P.S. I assume that you know that you have to load jQuery library first in your html file header, something like this:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>

And then put that jquery code inside of script tags:

<script>
...that jQuery code...
</script>

UPDATED BY REQUEST - complete script version

<html>
<head>
<title>Control Page</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
    <script>
        //we need document.ready call to make sure that DOM loaded before we try to bind anything
        $(document).ready(function() {
            //Here we bind click event
            $('#mysubmit').bind('click', function(e){e.preventDefault();
                //This is what happens on click - we send AJAX request
                var jqueryXHR = $.ajax({
                    'type': 'POST',
                    'url': 'http://localhost/killIE.php',
                    'dataType': 'json'
                   });
                //And when AJAX requests complete(succeeded or failed) - we update text
                jqueryXHR.complete(function(){  
                    $('span#result').html('Oh no!!! the End is neearh! erver is shutting DoWn.')
                });
            })
        });
    </script>
</head>
<body>
    <input type="submit" id="mysubmit" value="Kill da server!" style = "margin-left:100px; margin-top:100px;" />
    <br/>
    <br/>
    <br/>
    <span id = "result">Not clicked yet - good!</span>

</body>
</html>

Upvotes: 7

Thomas Winsnes
Thomas Winsnes

Reputation: 1961

Here is a quick tutorial that can teach you how to do this: http://aleembawany.com/2005/09/01/ajax-instant-tutorial

Though, I have to say that it's not very safe to have a public php file that has the power to shut down your server. If the wrong people got a hold of the url...

Upvotes: 0

Dixit Wadhwani
Dixit Wadhwani

Reputation: 227

do one thing.. That when you click on to the shutdownm button just open this page on next page dont open in same page and execute it.. And close the new page after some period.. So u dont need to reload the page.. Hope this work.. Try this..

Upvotes: -1

jamierushad
jamierushad

Reputation: 91

Why not just open the php file in a popup? I suppose if you really want to get fancy you can load it into a modal popup. I take it whatever "shutDown.php" is that it just runs on page load. So it probably doesn't matter too much how you call it.

<html>
 <head>
 <script type="text/javascript">
 function open_win()
 {
 window.open("http://www.url.com/shutDown.php")
 }
 </script>
 </head>
 <body>

<input type=button value="Shut Down" onclick="open_win()" />

</body>
 </html>

Upvotes: 0

Hassek
Hassek

Reputation: 8995

If you want to do it via javascript you should do an Ajax call, I would recommend you to use jQuery for it, but it can be done only with javascript if you want.

Hope this helps.

Upvotes: 0

Pwnna
Pwnna

Reputation: 9538

What you're looking for is called AJAX. Search it up!

Upvotes: -1

Related Questions