2011-07-02 33 views
0

我需要一些幫助,使用按鈕onClick使用onclick按鈕更新數據庫信息

<button type="button" id="1" onclick="this.disabled=true; ^FUNCTION TO UPDATE^"> 
<button type="button" id="2" onclick="this.disabled=true; ^FUNCTION TO UPDATE^"> 
<button type="button" id="3" onclick="this.disabled=true; ^FUNCTION TO UPDATE^"> 

有了這個代碼,我禁用按下的按鈕,但我還需要更新數據庫中的信息發送ID,這樣的事情:

UPDATE reservation SET status='approved' WHERE id=^ID OF THE CLICKED BUTTON^ 

我需要它在加載不會發送到POSTGET

+0

看看jQuery,更具體地說它是[Ajax](http://api.jquery.com/category/ajax/)功能。這正是你想要的。 –

+0

「我需要將它加載到同一頁面,而不用POST或GET發送它。」 - 爲什麼這個要求?無論是通過明確的POST(GET都不應該用於此目的...),還是通過Ajax,它都是您可以在數據庫中更新信息的相同方法。 –

回答

1

使用AJAX調用此例如:

$.get('update_reservation.php', {id: 1}); 

看到http://api.jquery.com/jQuery.get/獲取更多信息。

+0

我不像我想要的那樣熟悉jQuery。這個調用可以直接放在onClick屬性中嗎? –

0

用普通的JavaScript,你可以做這樣的事情:

<script type="text/javascript"> 
<!-- 
var page = "dbupdate.php"; // hardcode this elsewhere out of sight 

function update(target,data){ 
document.getElementById(target).innerHTML = 'sending...'; 
if (window.XMLHttpRequest) { 
     req = new XMLHttpRequest(); 
     req.onreadystatechange = function() {ajaxDone(target);}; 
     req.open("POST", page, true); 
     req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
     req.send("data=" + data); 
    // IE/Windows ActiveX version 
    } else if (window.ActiveXObject) { 
     req = new ActiveXObject("Microsoft.XMLHTTP"); 
    if (req) { 
      req.onreadystatechange = function() {ajaxDone(target);}; 
      req.open("POST", page, true); 
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
      req.send("data=" + data); 

     } 
    } 
} 

function ajaxDone(target) { 
    // only if req is "loaded" 
    if (req.readyState == 4) { 
     // only if "OK" 
     if (req.status == 200 || req.status == 304) { 
      results = req.responseText; 
      document.getElementById(target).innerHTML = results; 
     } else { 
      document.getElementById(target).innerHTML="ajax error:\n" + 
      req.statusText; 
     } 
    } 
} 
// --> 
</script> 

對於輸出確保你有一個id等於目標股利。