javascript 如何在php上使用ajax插入数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17133970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert into database using ajax on php?
提问by
I have html form with three values fname,lname,age. I want to send it on server side php file and insert into database.
我有三个值 fname,lname,age 的 html 表单。我想将它发送到服务器端 php 文件并插入到数据库中。
My html form is like this:
我的html表单是这样的:
<html>
<head>
<script>
function insert(fname,lname,age)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax_db_php.php?fname=fname&lname=lname&age=age",true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<table>
<tr><td>First Name : </td><td> <input type="text" fname="fname"/> </td> </tr>
<tr><td>Last Name : </td><td> <input type="text" fname="lname"/> </td> </tr>
<tr><td>City : </td><td> <input type="text" fname="age"/> </td> </tr>
<input type="submit" onclick="insert(fname,lname,age)">
</table>
</form>
</body>
</html>
As I have used ajax it should not load entire page, but when I click submit button it load entire page. why? And php page which receives values and insert into database is:
由于我使用了 ajax,它不应该加载整个页面,但是当我单击提交按钮时,它会加载整个页面。为什么?接收值并插入数据库的php页面是:
<html>
<body>
<?php
$fname=$_GET['fname'];
$lname=$_GET['lname'];
$age=$_GET['age'];
//echo "firstname : " $fname;
$con=mysqli_connect('127.0.0.1:3306' ,'root','root','my_db');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO table1 (fname, lname, age)
VALUES
('$_POST[fname]','$_POST[lname]','$_POST[age]')";
$result=mysql_query($sql);
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
</body>
</html>
When I click the submit button it shows nothing, no error and no update in database too.
当我单击提交按钮时,它什么也没显示,也没有错误,数据库中也没有更新。
采纳答案by Vivek Muthal
Change as said by Nikola then Change in HTML
正如 Nikola 所说的那样改变,然后在 HTML 中改变
input type="submit"
to
到
input type="button"
This will not reload the page. Then Remove below tags from PHP
这不会重新加载页面。然后从 PHP 中删除以下标签
<html>
<body>
</html>
</body>
I'll recommend use jQuery to make ajax calls.
我会推荐使用 jQuery 进行 ajax 调用。
Update how to use ajax and serialize:
更新如何使用ajax和序列化:
This is test.php
这是 test.php
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js" ></script>
<script>
$(document).ready(function(){
$("form").on('submit',function(event){
event.preventDefault();
alert("Hello");
data = $(this).serialize();
$.ajax({
type: "GET",
url: "test2.php",
data: data
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
});
</script>
</head>
<body>
<form>
<table>
<tr><td>First Name : </td><td> <input type="text" name="fname"/> </td> </tr>
<tr><td>Last Name : </td><td> <input type="text" name="lname"/> </td> </tr>
<tr><td>City : </td><td> <input type="text" name="age"/> </td> </tr>
<input type="submit" value="Submit" />
</table>
</form>
</body>
</html>
this is test2.php
这是 test2.php
<?php
if($_GET)
{
$fname=$_GET['fname'];
$lname=$_GET['lname'];
$age=$_GET['age'];
echo "firstname : ".$fname;
}
?>
回答by Nikola
You set $fname
, $lname
and $age
correctly, but you never use them, instead of that you use $_POST
variables which do not exist.
您设置$fname
,$lname
并且$age
正确,但您从不使用它们,而不是使用$_POST
不存在的变量。
Instead of
('$_POST[fname]','$_POST[lname]','$_POST[age]')";
代替
('$_POST[fname]','$_POST[lname]','$_POST[age]')";
you should use the $_GET
variables e.g.
你应该使用$_GET
变量,例如
('$fname','$lname','$age')";
('$fname','$lname','$age')";
I'd also suggest you to escape the strings when adding them to the database. One possible solution can be Prepared statements.
我还建议您在将字符串添加到数据库时对其进行转义。一种可能的解决方案是Prepared statements。