簡體   English   中英

AJAX 未將變量傳遞給 PHP 文件

[英]AJAX not passing variable to PHP file

我是 PHP 的半新手,我正在處理網站的儀表板頁面,管理員可以在其中查看所有現有管理員,每個管理員名稱的行上都有一個按鈕,應該檢查每個管理員的權限(訪問文章編輯,客戶付款信息等)。

當管理員單擊該按鈕時,頁面中會滑出一個 div,並顯示此 window:

管理員權限對話框

它顯示當前權限的位置。

我有一個名為“ 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

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);

?>

這是我在 get 方法中請求 ajax 從數據庫接收結果的頁面文件:

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!");
                };
            };


        };
    });

當我單擊“檢查權限”按鈕時,我在瀏覽器的控制台中收到此錯誤:

JSON 控制台錯誤

JSON 控制台錯誤跟蹤?

我知道問題是$_GET['admin_id']是空的,所以它沒有收到任何值,因為如果我使用現有的 admin_id,我會在頁面中得到結果並且一切正常。

我究竟做錯了什么? 我在網上讀到我不需要使用 post 方法來執行此操作,但無論如何我都嘗試過,但沒有任何效果。

提前致謝

open的第三個參數是是否應該異步處理請求。 這不是你放置數據的地方。

如果您正在發出 POST 請求,那么您會將數據作為第一個參數放入send方法……但這不接受普通的 object。

由於您正在發出 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