将数据发送到数据库时,在不刷新页面的链接点击发送到、页面、链接、数据库

2023-09-10 21:41:43 作者:眼眉迷人.

有没有办法将数据发送到数据库时,点击无页面的链接刷新?

Is there a way to send data to database when click on a link without page refresh?

我用PHP / MySQL的...

I use php/mysql...

推荐答案

我会给你使用jQuery的例子; 比方说,我们有一个属性ID =button_id的链接。 (你必须学会​​的jQuery选择)。

I will give you an example using jQuery; Let's say that we have a link with an attribute id="button_id". ( You have to learn the jQuery selectors ).

    $("#button_id").click(function(){
    var var_data = 5;
    $.ajax({
            url: "my_script.php",
            data: { var_PHP_data: var_data };
            success: function(data) {
                // do something;
                                  alert(data);
            },
     });
});

说明: 您可以发送变量 var_data 名为 var_PHP_data 到 my_script.php 不使用Ajax调用页面刷新。 (使用GET方法)。 这是很简单的东西,你必须在你的PHP脚本编写的例子。

Explanation: You will send the variable var_data with the name var_PHP_data to a my_script.php without page refresh using an ajax call. ( using GET method ). this is very simple example of what you have to write on your PHP script.

<?php
$var_name = $_GET['var_PHP_data'];
echo 'This is what you have send'.$var_name;

?>

由于默认的方式发送给在jQueyr的 AJAX功能变量是GET 。 我们必须使用$ _GET函数在PHP。 这个PHP脚本将打印一条消息,该消息将在成功处理:在Ajax调用函数和公正的例​​子中,我们会提醒从PHP返回此消息

Because the default method to send variables in the ajax function in jQueyr is GET. We have to use the $_GET function in PHP. This php script will print a message and this message will be handled in the success: function in the Ajax call and just for example we will alert this message returned from PHP.