Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Transfer javascript variable to bash script

    I have this code to prompt for text input and then I want to run script.sh:

    Code:
     <script type="text/javascript">
    function runscript()
    {
    var x=prompt("Enter text:");
    //this is where I'm lost
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","/cgi-bin/script.sh");
    xmlhttp.send()
    }
    </script>
    Is it possible to substitute x above as the value for $txt in script.sh which looks like this below and how please?

    Code:
     #!/bin/bash
    
    echo "Content-type: text/html"
    echo ""
    
    <some xmlrpc command> "add text $txt"
    Last edited by jonhen; 01-19-2012 at 04:45 PM. Reason: grammar

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,455
    Thanks
    0
    Thanked 632 Times in 622 Posts
    You need to pass the value either using GET or POST and then read it in from that spot on the server.

    If you use GETthe javaScript to send the value would be:

    xmlhttp.open("GET","/cgi-bin/script.sh?x="+encodeURI(x));
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for the input.

    Well I tried that but I can't get script.sh to read x. I've checked my Apache2 error log and there's nothing showing up other than the deliberate errors I put in to make something show up. I know the script works but maybe something I'm not doing right.

    It's almost as if it's working but x is being missed.

    Last edited by jonhen; 01-18-2012 at 08:04 PM. Reason: grammar

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,455
    Thanks
    0
    Thanked 632 Times in 622 Posts
    You might need to ask about how to read in the querystring in a forum that deals with that server side language.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    jonhen (01-19-2012)

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Yes I believe your right. Thanks for getting me this far. I have tried a number of things but with no success.



    Jon

  • #6
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Just to add, the method I used to solve this problem was to change my script.sh to perl with a few extra lines which together with your line of code suggested yesterday solved my problem.

    Thanks.

    Jon


  •  

    Posting Permissions

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