Results 1 to 6 of 6

Thread: Simple javascript to cgi call help

  1. #1
    Join Date
    Nov 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple javascript to cgi call help

    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

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <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:
    Code:
    callCGI(var1, var2, var3, ...)
    The only limitation is that the correct number of hidden elements must exist in the form.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Nov 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I see. You want something "silent" using AJAX?
    Code:
    var xh = new XMLHttpRequest();
    xh.open("GET", "Send.cgi?Var1=" + var1 + ",Var2=" + var2 + ",Var3=" + var3, false);
    xh.send(null);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Nov 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That was an example. There are several different XMLHttpRequest object equivalents you must accomodate if you want the code to work with IE.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •