微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我需要有关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 @R_108_4045@ion 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 @R_108_4045@ion 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;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐