Update database info with onclick button

后端 未结 2 1491
深忆病人
深忆病人 2021-01-28 20:41

I need some help with a button onClick.

相关标签:
2条回答
  • 2021-01-28 21:15

    Use ajax call for this eg:

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

    see http://api.jquery.com/jQuery.get/ for more info.

    0 讨论(0)
  • 2021-01-28 21:34

    With plain javascript you can do something like this:

    <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>
    

    For output make sure you have a div with the id equal to target.

    0 讨论(0)
提交回复
热议问题