简体   繁体   English

AJAX 未将变量传递给 PHP 文件

[英]AJAX not passing variable to PHP file

I'm semi-new to PHP and I'm working on a website's dashboard page where an administrator can see all existing admins, there's a button on each admin name's row that's supposed to check the privileges of each admin (access to article editing, clients payment information, etc).我是 PHP 的半新手,我正在处理网站的仪表板页面,管理员可以在其中查看所有现有管理员,每个管理员名称的行上都有一个按钮,应该检查每个管理员的权限(访问文章编辑,客户付款信息等)。

When the admin clicks on that button, a div slides in the page and it shows this window:当管理员单击该按钮时,页面中会滑出一个 div,并显示此 window:

Admin Privileges Dialog管理员权限对话框

Where it shows the current permissions.它显示当前权限的位置。

I have a var called " admin_id " which is updated whenever a button is clicked, because as I'm using "foreach" to iterate the list of admins, I also have their " admin_id " linked to each of these buttons.我有一个名为“ admin_id ”的变量,每当单击按钮时它都会更新,因为当我使用“foreach”来迭代管理员列表时,我也将他们的“ admin_id ”链接到这些按钮中的每一个。

My point is to use ajax so an admin can change another admin's permissions without reloading the page and reopening this little dialog so I'm trying to pass the variable " admin_id " to my PHP file where I have my SQL and returning it My point is to use ajax so an admin can change another admin's permissions without reloading the page and reopening this little dialog so I'm trying to pass the variable " admin_id " to my PHP file where I have my SQL and returning it

check_privileges.php check_privileges.php

<?php

    include("../../includes/dbconnect.php");


    $admin_id = $_GET['admin_id'];


    $sql = "SELECT * FROM admins WHERE admin_id = $admin_id";
    $sql_query = mysqli_query($dbconnect, $sql);

    // storing results in array
    $data = array();
    while ($row = mysqli_fetch_array($sql_query)){

        $data[] = $row;
    }

    // returning response in JSON format
    echo json_encode($data);

?>

and this is the page's file where I'm requesting the ajax, in get method, to receive the results from the database:这是我在 get 方法中请求 ajax 从数据库接收结果的页面文件:

users.php users.php

$(document).ready( function(){

    // We now have the admin_id of the user correspondent to the row we're clicking, declaring the admin_id variable outside of the scope of the function so I can access it anywhere

    var admin_id;
// update the variable value with the value of the admin_id of the button
    $(".row-user-perm").find("button").click( function() {
        var admin_id = $(this).val();
        console.log(admin_id);

    });

    $('.button-priv').click( function(){

        var ajax = new XMLHttpRequest();
        var method = "get";
        var url = "includes/check_privileges.php";
        var data = {admin_id: admin_id};
        var asynchronous = true;
        var success = function (data) {
                    console.log(data.report.data[0].article_flag);
                };

        ajax.open(method, url, data, asynchronous);
        // sending ajax request
        ajax.send();

        // receiving response from data.php
        var data = {};
        ajax.onreadystatechange = function(){
            if (this.readyState == 4 && this.status == 200){
                
                // converting JSON back to array
                var data = JSON.parse(this.responseText);
                console.log(data); // for debugging
                // alert(data[0].article_flag);

                var article_flag = data[0].article_flag;
        
                if (article_flag == 1) {
                    console.log("Article flag is 1!");
                    $('#article-perm-svg').removeClass('no-perm');
                    $('#article-perm-svg').addClass('has-perm');
                }else{
                    alert("article flag is not 1!");
                };
            };


        };
    });

And when I click a "Check Privileges" button, I get this error in the browser's console:当我单击“检查权限”按钮时,我在浏览器的控制台中收到此错误:

JSON console error JSON 控制台错误

JSON console error trace? JSON 控制台错误跟踪?

I know the problem is the $_GET['admin_id'] being empty so it's not receiving any value because if I use an existing admin_id, I get the results in the page and everything working.我知道问题是$_GET['admin_id']是空的,所以它没有收到任何值,因为如果我使用现有的 admin_id,我会在页面中得到结果并且一切正常。

What am I doing wrong?我究竟做错了什么? I've read online that I don't need to use a post method to do this, but I've tried both anyway and nothing worked.我在网上读到我不需要使用 post 方法来执行此操作,但无论如何我都尝试过,但没有任何效果。

Thanks in advance提前致谢

The third argument to open is if the request should be handled asynchronously or not. open的第三个参数是是否应该异步处理请求。 It isn't where you put the data.这不是你放置数据的地方。

If you were making a POST request, then you would put the data in as the first argument to the send method… but that doesn't accept a plain object.如果您正在发出 POST 请求,那么您会将数据作为第一个参数放入send方法……但这不接受普通的 object。

Since you are making a GET request, you need to encode the data into the query string of the URL that you pass as the second argument to open .由于您正在发出 GET 请求,因此您需要将数据编码到您作为第二个参数传递给open的 URL 的查询字符串中。

 const admin_id = "12345"; const relativeUrl = 'includes/check_privileges.php'; const url = new URL(relativeUrl, location.href); url.searchParams.append('admin_id', admin_id); console.log(url.toString())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM