2017-04-21 64 views
-7

如何在HTML按鈕上觸發一個sql查詢?在HTML按鈕上觸發一個sql請求

+1

具有u試圖[谷歌](https://www.google.pl/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=Trigger+a + SQL + +上的+ HTML +按鈕請求+)? – Arkej

+0

使用Ajax請求,並用您的sql請求調用php腳本 –

回答

1

我會使用jQuery。

$("#submit").on("click", function(){ 
 
    $.ajax({ 
 
\t \t \t type: "post", 
 
\t \t \t url: "process.php", 
 
\t \t \t data:"action=SomeActionHere", 
 
\t \t \t success: function(data){ 
 
\t \t \t \t alert("SUCCESS"); 
 
\t \t \t }, 
 
\t \t \t error: function(e){ 
 
\t \t \t \t alert("ERROR"); 
 
\t \t \t } 
 
\t \t }); 
 
};
<!--index.html --> 
 
<button id="submit">GOGO</button> 
 

 
<!--process.php--> 
 

 
<?php 
 
header("Content-type: application/json; charset=utf-8"); 
 
//connecto to server 
 

 
$action = $_POST['action']; 
 

 
if($action == "SomeActionHere"){ 
 

 
// do you sql stuff here 
 

 
// when done sql 
 
echo json_encode("SUCCESS"); // whatever data you want to send back 
 

 
} 
 

 
?>