[英]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>
I need that when I click "newUser" the value of the name is saved into my mysql database without refreshing the page. 我需要在单击“ newUser”时将名称的值保存到我的mysql数据库中,而不刷新页面。 Any exemple of how to do this? 如何执行此操作的任何示例?
If you don't want to use a <form>
and a submit
button which would be the absolutely correct way to do this, you will need to use javascript and AJAX: 如果您不想使用<form>
和submit
按钮(这是绝对正确的方法),则需要使用javascript和AJAX:
Cancel the default action of the link 取消链接的默认操作
Add User 添加用户
and the insert
function could be defined in a separate javascript file: 并且insert
函数可以在单独的javascript文件中定义:
function insert() {
var name = document.getElementById('name').value;
// TODO: send an AJAX request to the server
return false;
}
http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/
$('#newUser').click(function(ev){
$.ajax(...);
ev.preventDefault();
});
You can do an Ajax
call with JQuery that will send the name to the server. 您可以使用JQuery进行Ajax
调用,该调用会将名称发送到服务器。 In your PHP code you will be able to create the Insert Query
in your user table. 在您的PHP代码中,您将能够在用户表中创建Insert Query
。
This can be done on the client side with jquery's ajax libraries to create the refreshless submit. 可以使用jquery的ajax库在客户端完成此操作,以创建不刷新的提交。 Here's an example of some html and javascript 这是一些html和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>
On the php side just code like you would if you were submitting a form via post. 在php方面,就像您通过邮寄提交表单一样,只需编写代码即可。 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站点上查找更多信息。
You can use JavaScript for that. 您可以为此使用JavaScript。 Take a look at jQuery
ajax: http://api.jquery.com/jQuery.ajax/ 看看jQuery
ajax: http : //api.jquery.com/jQuery.ajax/
note: next time try googling, or at least provide what you have tried 注意:下次尝试谷歌搜索,或至少提供您尝试过的内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.