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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.