php - 使用ajax从html表单更新数据库

标签 php html ajax

我需要 ajax 方面的帮助。我想更新一个将更新数据库的 php 文件。我有一个表单,它将选中的复选框发送到一个 php 文件,然后更新数据库。我想用 ajax 来做这个,但我正在努力解决这个问题。我知道如何更新 <div> ajax 的 HTML 元素,但无法解决这个问题。

HTML 脚本

<html>
<head>
    <script src="jquery-3.1.0.min.js"></script>
</head>

<body>
<form name="form">
<input type="checkbox" id="boiler" name="boiler">
<input type="checkbox" id="niamh" name="niamh">
<button onclick="myFunction()">Update</button>
</form>
<script>
function myFunction() {
    var boiler = document.getElementByName("boiler").value;
    var niamh = document.getElementByName("niamh").value;
// Returns successful data submission message when the entered information is stored in database.
var dataString = 'boiler=' + boiler + 'niamh=' + niamh;

// AJAX code to submit form.
    $.ajax({
    type: "POST",
    url: "updateDB.php",
    data: dataString,
    cache: false,
    success: function() {
        alert("ok"); 
    }
    });
}

</script>
</body>
</html>

PHP 更新数据库.php

<?php

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="14Odiham"; // Mysql password 
$db_name="heating"; // Database name 
$tbl_name = "test";

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$boiler = (isset($_GET['boiler'])) ? 1 : 0;
$niamh = (isset($_GET['niamh'])) ? 1 : 0;

// Insert data into mysql 
$sql = "UPDATE $tbl_name SET boiler=$boiler WHERE id=1";
$result = mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Successful";
echo "<BR>";
}

else {
echo "ERROR";
}
?>
<?php
//close connection
mysql_close();
header ('location: /ajax.php');
?>

我希望在不刷新页面的情况下进行更新。

最佳答案

我只是想要一些建议,首先你的 html 页面代码应该像-

<html>
<head>
    <script src="jquery-3.1.0.min.js"></script>
</head>

<body>
<form name="form" id="form_id">
<input type="checkbox" id="boiler" name="boiler">
<input type="checkbox" id="niamh" name="niamh">
<button onclick="myFunction()">Update</button>
</form>
<script>
function myFunction() {
   // it's like cumbersome while form becoming larger  so comment following three lines        
      // var boiler = document.getElementByName("boiler").value;
     // var niamh = document.getElementByName("niamh").value;
     // Returns successful data submission message when the entered information is stored in database.
    //var dataString = 'boiler=' + boiler + 'niamh=' + niamh;

// AJAX code to submit form.
    $.ajax({
    // instead of type use method
    method: "POST",
    url: "updateDB.php",
    // instead  dataString i just serialize the form like below this serialize function bind all data into a string so no need to worry about url endcoding
    data: $('#form_id').serialize(),
    cache: false,
    success: function(responseText) {
        // you can see the result here
        console.log(responseText)
        alert("ok"); 
    }
    });
}

</script>
</body>
</html>

现在我转向 php 代码: 你在 php 中使用了两行代码

$boiler = (isset($_GET['boiler'])) ? 1 : 0;
$niamh = (isset($_GET['niamh'])) ? 1 : 0;

$_GET 用于 get 方法,$_POST 用于 post 方法,因此您在 ajax 中使用 post 方法,上面的代码行应该是这样的

$boiler = (isset($_POST['boiler'])) ? 1 : 0;
$niamh = (isset($_POST['niamh'])) ? 1 : 0;

关于php - 使用ajax从html表单更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39481164/

相关文章:

javascript - jQuery 点击功能不起作用

html - 看看 Container 的第一个元素的边距错误!并为此给出适当的理由

javascript - Thumbs_up Gem,ajax 的一个小问题

php - 在 url 中使用破折号时路由不起作用

php - send方法总是返回0

php - Float left 在 Drupal 表单中无法正常工作

javascript - Tapestry 5.3 如何使用按钮检索文本字段值但不提交表单

javascript - 使用无逻辑模板系统将逻辑放在哪里

javascript - 取消选择 DataTables 中的所有选定行

javascript - 在通过 ajax 创建的页面元素上运行 javascript