我需要有关ajax的帮助.我想更新一个php文件,它将更新数据库.我有一个表格,将选中的复选框发送到一个php文件,然后更新数据库.我想用ajax做到这一点,但是我为此感到挣扎.我知道如何更新 div HTML元素由Ajax提供,但无...
我需要有关ajax的帮助.我想更新一个php文件,它将更新数据库.我有一个表格,将选中的复选框发送到一个php文件,然后更新数据库.我想用ajax做到这一点,但是我为此感到挣扎.我知道如何更新< div> HTML元素由Ajax提供,但无法解决.
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 updateDB.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表单更新数据库
猜你喜欢
- 分页技术原理与实现之无刷新的Ajax分页技术(三) 2023-01-20
- Servlet+Ajax实现智能搜索框智能提示功能 2023-02-01
- echarts图形x、y坐标文字设置间隔显示及相关问题 2022-08-30
- Ajax配合Spring实现文件上传功能代码 2023-02-01
- ajax异步实现文件分片上传实例代码 2023-02-23
- vue实现三级页面跳转功能 2023-07-09
- ES6中async函数与await表达式的基本用法举例 2022-08-30
- vue中的条件渲染 v-show、v-if、v-else、v-else-if 2023-10-08
- 解决ajax返回验证的时候总是弹出error错误的方法 2022-12-15
- ajaxFileupload实现多文件上传功能 2023-02-14