简体   繁体   English

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>

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:

  1. Subscribe for the onclick event of the anchor 订阅锚点的onclick事件
  2. Send an AJAX request to the server passing the value of the name entered in the textbox 向服务器发送AJAX请求,并传递在文本框中输入的名称值
  3. 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.

相关问题 使用多个 forms 将数据插入 DB 使用 Jquery &amp; PHP 无需刷新页面 - Using multiple forms to insert data to DB using Jquery & PHP without refreshing the page 如何使用AJAX和LARAVEL在MySQL中插入数据而不刷新页面 - How to insert data in MySQL without refreshing the page, using AJAX and LARAVEL PHP,MySQL和JQuery-更新表而不刷新页面 - PHP, MySQL and JQuery - Updating a table without refreshing page 从模态插入数据而不刷新页面 - Insert data from modal without refreshing the page 如何使用AJAX将数据插入mysql数据库(不刷新整个页面) - How to insert data into mysql database using AJAX (without refreshing whole page) php使用jquery获取数据而不刷新 - php fetching data using jquery without refreshing 如何在没有页面刷新的情况下使用jQuery和Ajax在MySQL中插入数据 - How to insert data in MySQL with jQuery and Ajax without page refresing 使用选择框以html格式显示mysql数据 <li> 标记而无需使用jQuery刷新页面 - use selectbox to show mysql data in html <li> tag without refreshing page with jquery 尝试在不使用php和ajax刷新页面的情况下将数据输入mysql数据库 - Trying to enter data into mysql database without refreshing the page using php and ajax 使用PHP和jQuery更新多个页面元素而无需刷新页面 - Updating multiple page elements without refreshing the page using PHP & jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM