前端:
<!DOCTYPE html> <html> <head> <meta charset="utf8"> <title>test</title> <script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js"></script> <script type="text/javascript"> function myClick () { $.ajax({ type: "POST", url: "index.php", data: {test : "123456789"}, dataType: 'text', success: function (data, status, jqXHR) { console.log(data); console.log(status); console.log(jqXHR); }, error: function (status) { console.debug(status); } }); } </script> </head> <body> <button onclick="myClick ()">Start</button> </body> </html>
後端:
<?php if(isset($_POST['test'])){ echo "Receive:" . $_POST['test']; }else{ echo "No data."; } ?>
可能會有些安全性問題要考量
參考資料:
http://en.wikipedia.org/wiki/Ajax_(programming)
http://www.w3schools.com/ajax/
http://api.jquery.com/jquery.ajax/
http://www.php.net/manual/en/reserved.variables.post.php
http://tw2.php.net/isset