我需要有关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中的async属性值之同步和异步及同步和异步区别 2022-10-17
- Ajax实现登录案例 2023-02-23
- JavaScript求解最长回文子串的方法分享 2022-10-21
- layui数据表格搜索 2022-12-13
- 关于 css:在 R Shiny 中更改侧边栏菜单项颜色 2022-09-21
- JavaScript中的location、history、navigator对象实例介绍 2023-07-10
- 下载highlightjs-copy-button.js给网站代码加上copy复制按钮 2023-08-29
- wepy微信小程序框架加入版本更新提示 2022-10-29
- Typescript+Vue大型后台管理系统实战 2023-10-08
- ajax动态加载json数据并详细解析 2023-02-23