0

I have a web page with a switch (html & CSS) and a toggle function (JS) which changes the appearance of the switch as it is clicked. The toggle function has a 'standby' variable which tracks the status of the switch. I also have a form (JS) which POSTs the value of standby to a cgi script (python) which will control some lights. That all works! Except, I do not want the cgi file to respond with a new web page or even reload the current web page - I'd like to keep the page as it is - which does not seem possible with cgi! I'm guessing cgi is the wrong approach (& this is another dumb question!) Thanks in advance...

  • You're looking for ajax - http://en.wikipedia.org/wiki/Ajax_(programming) – aldux Jan 29 '15 at 19:15
  • Thanks for all the answers - I used JQuery and ajax as suggested and it has worked for me - simple when you know how! '$.ajax({ type: "POST", url: "/cgi-bin/hello.py", data: {status: standby} });' –  Jan 30 '15 at 01:31

3 Answers3

1

You can send your Form using Ajax. The easiest way is to use Jquery.

Here is a nice tutorial: http://hayageek.com/jquery-ajax-form-submit/

Fedaykin
  • 4,262
  • 3
  • 20
  • 32
1

You’ll need to make your request asynchronously using JavaScript.

Have a look at the XMLHttpRequest object or use a wrapper function like jQuery’s $.ajax method (especially if you want to support older browsers).

Please see this answer for an example of how to do a POST request using the XMLHttpRequest object.

Community
  • 1
  • 1
aaronk6
  • 2,345
  • 1
  • 16
  • 24
1

Thanks for all the answers - I appreciate you putting me on the right track. I used JQuery and ajax as suggested and it is working - simple when you know how!

This is in index.html:

    $.ajax({
    type: "POST",
    url: "/cgi-bin/standby.py",
    data: {status: standby}
});

and in standby.py:

 data = cgi.FieldStorage() 
 data = data["status"].value