jQuery的PHP插入数据到MySQL而不刷新页面 [英] jquery php insert data into mysql without refreshing the page
问题描述
您好,我的网站中有下一个代码:
Hello I have the next code into my website:
<input type="text" id="name" name="name" width="25px"/>
<a href="#" id="newUser">Add User</a>
我需要在单击"newUser"时将名称的值保存到我的mysql数据库中,而不刷新页面. 如何执行此操作的任何示例?
I need that when I click "newUser" the value of the name is saved into my mysql database without refreshing the page. Any exemple of how to do this?
推荐答案
这可以在客户端使用jquery的ajax库完成,以创建不刷新的提交. 这是一些html和javascript
This can be done on the client side with jquery's ajax libraries to create the refreshless submit. Here's an example of some html and javascript
<html>
<head>
<script type="text/javascript" src="jq/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
function adduser()
{
var data=$("#adduserform").serialize();
$.ajax({
type: "POST",
url: "adduser.php",
data: data,
dataType: "html",
});
}
</script>
</head>
<body>
<form id="adduserform" name="adduserform">
<fieldset>
<input type="text" id="name" name="name" width="25px"/>
</fieldset>
</form>
<a href="javascript:adduser();" id="newUser">Add User</a>
</body>
</html>
在php端,只需编写代码,就像通过邮寄提交表单一样. 我还没来得及尝试,但我希望它能起作用:) 就像其他人建议的那样,请在jquery网站上查找更多信息.
On the php side just code like you would if you were submitting a form via post. I haven't had time to try it, but I hope it works :) Like the others have suggested, look on the jquery site for more info.
这篇关于jQuery的PHP插入数据到MySQL而不刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!