...

View Full Version : Resolved Run server side script from javascript



jonhen
01-14-2012, 01:25 AM
I'm trying to get a server side script to run from a webpage but I just can't get it to run.
I've run this:



<html>
<head>
<script type="text/javascript">
function runscript()
{
alert(“Hello”)
}
</script>
</head>
<body>
<li><a href="javascript required.html" onclick="runscript(); return false;" title="Command">RUN</a></li>
</body>
</html>


and it works ok on both Linux and Windows clients. I've tried this but to no avail:



<html>
<head>
<script type="text/javascript">
function runscript()
{
exec(“/cgi-bin/script.sh”)
}
</script>
</head>
<body>
<li><a href="javascript required.html" onclick="runscript(); return false;" title="Command">RUN</a></li>
</body>
</html>


Here's the script just in case this is the issue but I doubt it:



#!/bin/bash

echo "Content-type: text/html"
echo ""

<some xmlrpc command>

I'm no coder, this is what I've gleaned from hours of surfing and learning. Any help gratefully accepted.

Edit: I'm using Apache2 on a Linux Mint 9 LXDE server and the script runs fine using:



<li><a href="/cgi-bin/script.sh" title="Script">RUN</a></li>


but I have to redirect back to the webpage it's called from and this causes a refresh which is why I'm trying to use javascript.

felgall
01-14-2012, 01:42 AM
To run something on the server from JavaScript you need to reference a file on the server that contains the code you want to run. This can be an image or another script file or something referenced in an iframe or object but is more usually referenced directly using an XMLHttpRequest call.

jonhen
01-14-2012, 10:38 AM
Hi Stephen thanks for your response and pointing me in the right direction. I had already explored that avenue unsuccessfully but I wasn't sure if that was the correct method or not. Anyway after a little perseverance I have now come up with this:



function runscript()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","/cgi-bin/script.sh");
xmlhttp.send();
}

which has solved my problem. Appreciate your help. Thanks. :thumbsup:

Jon



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum