DragonWar% 2018-07-09 18:58 采纳率: 0%
浏览 56

Ajax中的php全局变量

So i have a page called user.php as test

<?php 
$user = $_GET['userID'];
echo $user;
?>

then i have .js that uses ajax to add a comment on the profile which works but the php that it uses (select and insert) is in a file called profileComments.php

$.ajax({
        url: 'profileComments.php',
        method: 'POST',
        async: false,
        data: {
            display: 1,
            user: userID
        },
        success: function () {
            insertComments();
        }
    });

now i want to edit the select query of that profileComments.php file to only display those with the right userID

<?php
include "db.php";
$user = $_GET['userID'];

if (isset($_POST["display"])) {
  $comments= "SELECT * FROM comments";
  $query= mysqli_query($connection, $comments);

  while ($comments = mysqli_fetch_assoc($query)) { ?>
        <li>
            <?php echo $comments["content"]?>
        </li>
    <?php }
}

But the problem is when I edit it in

"SELECT * FROM comments WHERE userID = $user"

at the top i've written this $user = $_GET['userID']; but it gives me the unidentified error

How can i make this work?

  • 写回答

1条回答 默认 最新

  • weixin_33717117 2018-07-09 19:18
    关注

    You're using AJAX to send a POST request, not a GET request (See the method value of your ajax request). Therefore, all of the data in that AJAX request will be read into the $_POST superglobal of PHP.

    You also named that key 'user', not 'userID' (see the data value of your ajax request).

    Try:

    $user = $_POST['user'];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀