PDA

View Full Version : Simple javascript to cgi call help



cmatkin
03-24-2006, 07:15 AM
Hi,

I was after a simple javascript function that calls a cgi script like as follows.

When I call the function I want to pass 3 variables to it like
Var1,Var2,Var3.

The function then should call the following script.
Send.cgi?A=Var1,C=Var2,CL=Var3

Is this possible?
Sorry but I have not had much javascript programming.

Regards
Craig Atkin

Twey
03-24-2006, 09:45 AM
<form name="inform" action="Send.cgi" method="get">
<input type="hidden" name="A">
<input type="hidden" name="B">
<input type="hidden" name="C">
</form>
<script type="text/javascript">
function callCGI() {
var e = document.forms['inform'];
var f = new Array();
for(var i = 0; i < e.elements.length; i++)
if(e.elements[i].type.toLowerCase() == "hidden")
f.push(e.elements[i]);
for(var j = 0; j < f.length && j < arguments.length; j++) f[j].value = arguments[j];
e.submit();
}
</script>Call it like so:
callCGI(var1, var2, var3, ...)The only limitation is that the correct number of hidden elements must exist in the form.

cmatkin
03-24-2006, 11:52 AM
Thanks for the quick reply.
My webpage has a script running on and is changing the 3 variables.

I'm after a non posting method. I was not to clear on the format of the script.

EG: If Var1=23 and Var2=39 and Var3=230 then I'm after the following cgi call.
Send.cgi?A=23,C=39,CL=230

The cgi script then uses the info and returns some info for the website to use. This part works.

Regards
CA

Twey
03-24-2006, 05:20 PM
I see. You want something "silent" using AJAX?
var xh = new XMLHttpRequest();
xh.open("GET", "Send.cgi?Var1=" + var1 + ",Var2=" + var2 + ",Var3=" + var3, false);
xh.send(null);

cmatkin
03-25-2006, 09:57 PM
Thanks for the reply.
This looks exactly what I'm after.
When I run the code on the page it comes up with a 'XMLHttpRequest' is undefined error.

It this an easy one to fix?

regards
craig

Twey
03-25-2006, 10:14 PM
That was an example. There are several different XMLHttpRequest object equivalents you must accomodate if you want the code to work with IE.