javascript - How do I post form by clicking a checkbox without refreshing the page? -


i'm making home automation system raspberry pi , raspbian. i'm gonna design web page contains checkboxes control lights via relays. need web code contains checkbox. when click checkbox, php execute python code file controls relays. heard posting forms without refreshing page can done ajax , javascript. i'm not familiar languages. guys, please show me simple checkbox example when check it, make php exec() file , uncheck it, make php exec() file.

here example:

<!doctype html> <html>     <head>         <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>         <title>raspberry pi</title>     </head>     <body>         <input type="checkbox" class="iclass" />         <script>     $("input.iclass").click(function (e) {     e.preventdefault();     $.post("post.php", {param1: value, param2: value}); });         </script>     </body> </html> 

and post.php gives , alert('succesful!'); now. somehow it's not working.

make sure use jquery library. can use this:

$("input.checkbox-class").click(function (e) {     e.preventdefault();     $.post("/path/to/php/script.php", {param1: value, param2: value}); }); 

and source of /path/to/php/script.php have:

<?php     exec("system"); ?> 

a sample code full html be:

<!doctype html> <html>     <head>         <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>         <title>raspberry pi</title>     </head>     <body>         <input type="checkbox" />         <script>             $(document).ready(function (e) {                 e.preventdefault();                 $.post("/path/to/php/script.php", {param1: value, param2: value});             });         </script>     </body> </html> 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -