栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

我只想要一些建议,首先您的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 pre 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;


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/381348.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号