单击div标签,将值更新到数据库

时间:2019-01-28 07:11:08

标签: php mysqli

一旦用户单击div标签部分,则数据库应更新为1。

2 个答案:

答案 0 :(得分:1)

<button type="button">Click Me</button>

<script>
$(document).ready(function() {
$("button").click(function(){
  $.ajax({
    url:"php_page.php", //the page containing php script
    type: "POST", //request type
    success:function(result){
    alert(result);
    }
  });
});
})
</script>

Php页面-

echo "get code here to query code. insert update and delete";

答案 1 :(得分:1)

您需要为此使用Ajax()。

示例:

HTML:

<div onclick ="updateTab('<somedata like id of table>')"></div>

Javascript:

function updateTab(tableID){
   $.ajax({
      url  : "<php page>",
      type : "post",
      data : {sys_id : tableID},
      success : function(){
        alert("Updated");
     }
  });
}

PHP:

$conn = new mysqli("<dbserver>","<dbuser>","<dbpass>","<dbtarget>");
$sql_statement = "UPDATE `table_name` SET active = 1 WHERE id = '". $_POST["sys_id"] ."';";
$conn->query($sql_statement);