Jump to content

invoking scripts from button clicks


ksmatthews

Recommended Posts

HI Gurus,

 

I am trying to run unix system commands by linking them to button clicks ..

 

For example I can run a system command like this:

exec($command);

 

But how can I link this to a button click WITHOUT having to submit a form to the web server ?

 

THe following code (unsurprisingly) will not work

 

<INPUT class = "button" TYPE="submit" id="stop_button"  NAME="stop_button" VALUE="STOP"

onClick = "exec($command)" />

 

because onclick event should be tied to a javascript function.

 

Any suggestions ?

 

Steven M

Link to comment
https://forums.phpfreaks.com/topic/77338-invoking-scripts-from-button-clicks/
Share on other sites

You'll need to use AJAX if you want to avoid an entire page reload. If you google, you'll find plently of examples. Wether or not you'll find something showing exactly what you want to do, i dont know, but the process will be the same - the javascript will make a request to a php page which will run the system command for you.

put this in your html file

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
var ajaxRequest;  // The variable that makes Ajax possible!

try{
	// Opera 8.0+, Firefox, Safari
	ajaxRequest = new XMLHttpRequest();
} catch (e){
	// Internet Explorer Browsers
	try{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			// Something went wrong
			alert("Your browser broke!");
			return false;
		}
	}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
		document.[color=red]formname[/color].[color=red]status[/color].value = ajaxRequest.responseText;
	}
}
ajaxRequest.open("GET", "file.php", true);
ajaxRequest.send(null); 
}

//-->
</script>
Status:<input type="text" name="status" value="" disabled>
<INPUT class="button" TYPE="submit" id="stop_button"  NAME="stop_button" VALUE="STOP" 
onClick = "ajaxFunction();" /> 

 

Then create a php file which will run the command like this

 

<?php
$command = "command";
if(exec($command))
{
echo "Stopped";
}
else
{
echo "Error";
}
?>

 

Try that out.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.