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

编程入门 行业动态 更新时间:2024-10-25 02:30:59
本文介绍了使用ajax从html表单更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想要一些关于ajax的帮助。我想更新一个将更新数据库的php文件。我有一个表单将选定的复选框发送到一个php文件,然后更新数据库。我想用ajax做到这一点,但我正在为此付出努力。我知道如何通过ajax更新< div> Html元素,但是无法解决这个问题。 $ b HTML脚本

< html> < head> < script src =jquery-3.1.0.min.js>< / script> < / head> < body> < form name =form> < input type =checkboxid =boilername =boiler> < input type =checkboxid =niamhname =niamh> < button onclick =myFunction()>更新< / button> < / form> < script> 函数myFunction(){ var boiler = document.getElementByName(boiler).value; var niamh = document.getElementByName(niamh)。value; //当输入的信息存储在数据库中时,返回成功的数据提交消息。 var dataString ='boiler ='+ boiler +'niamh ='+ niamh; //提交表单的AJAX代码。 $ .ajax({ type:POST, url:updateDB.php, data:dataString, cache:false,成功:function(){ alert(ok); } }); } < / script> < / body> < / html>

PHP updateDB.php

<?php $ host =localhost; //主机名称 $ username =root; // Mysql username $ password =14Odiham; // Mysql密码 $ db_name =heating; //数据库名称 $ tbl_name =test; //连接到服务器并选择数据库。 mysql_connect($ host,$ username,$ password)或die(无法连接); mysql_select_db($ db_name)或死(无法选择DB); $ boiler =(isset($ _ GET ['boiler']))? 1:0; $ niamh =(isset($ _ GET ['niamh']))? 1:0; //将数据插入到mysql中 $ sql =UPDATE $ tbl_name SET boiler = $ boiler WHERE id = 1; $ result = mysql_query($ sql); //如果成功地将数据插入到数据库中,则显示消息成功。 if($ result){ echoSuccessful; 回显< BR>; } else { echoERROR; } ?> <?php //关闭连接 mysql_close(); header('location:/ajax.php'); ?>

我希望在更新页面时进行更新。

解决方案

我只是想一些建议,并首先你的HTML页面代码应该喜欢 -

< HTML> < head> < script src =jquery-3.1.0.min.js>< / script> < / head> < body> < form name =formid =form_id> < input type =checkboxid =boilername =boiler> < input type =checkboxid =niamhname =niamh> < button onclick =myFunction()>更新< / button> < / form> < script> 函数myFunction(){ //这样看起来很麻烦,而表单越来越大,所以在三行后注释 // var boiler = document.getElementByName(boiler).value; // var niamh = document.getElementByName(niamh)。value; //当输入的信息存储在数据库中时,返回成功的数据提交消息。 // var dataString ='boiler ='+ boiler +'niamh ='+ niamh; //提交表单的AJAX代码。 $ .ajax({ //代替类型使用方法方法:POST, url:updateDB.php, //而不是dataString我只是序列化如下这个序列化函数的形式将所有数据绑定到一个字符串,所以不需要担心URL终止代码 data:$('#form_id')。serialize(), cache:false, success:function(responseText){ //你可以在这里看到结果 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方法,因此您使用post方法ajax和上面的代码应该像

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

I would like some help with ajax. I would like to update a php file which will update a database. I have a form which send the selected check box to a php file which then updates the data base. I would like to do this with ajax but I am struggling with this. I know how to update <div> Html elements by ajax but cannot work this out.

HTML script

<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'); ?>

I would like this to update with out refreshing the page.

解决方案

I just want some suggestion and first your html page code should like-

<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>

Now i am turning to php code: You used two line of code right in php

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

$_GET is used in get method and $_POST for post method, thus you are using post method in ajax and above line of code should be like

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

更多推荐

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

本文发布于:2023-08-05 20:54:49,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   数据库   ajax   html

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!