douganmo1121 2017-06-06 19:52
浏览 47

Ajax - 注意:PHP中使用$ _GET时的未定义索引[重复]

I'm creating for a school assignment a sort of social media platform. I've created a simple chat tool. Each friend relation has a single conversation.

The problem is that when I reload all the chat messages using Ajax, I can't use the $_GET method to get the conversation id which I saved in the url. Without this id I can't use the function in my Chat class. I'm sorry if some code is written in Dutch.

The url looks like this chat.php?gesprek_id=1. Gesprek or Conversation Id is like I said saved in the url. After the Ajax request I receive the notice:

'Notice: Undefined index: gesprek_id in ...'

It refers to the $_GET method in php.

My code:

Chat Class:

public function getNewMessages($gesprekId){
    $conn = Db::getInstance();
    $stm = $conn->prepare("SELECT * from chat WHERE gesprek_id = :gesprek_id AND tijd > :tijd");
    $stm->bindValue(":gesprek_id", $gesprekId, PDO::PARAM_STR);
    $stm->bindValue(":tijd", $_SERVER['REQUEST_TIME'], PDO::PARAM_STR);
    $res = $stm->fetchAll();
    return $res;

Php for Ajax

header('Content-Type: application/json');



$gesprekId = $_GET['gesprek_id'];

$chat = new \Kvm\Chat();
$newMessages = $chat->getNewMessages($gesprekId);

<?php foreach ($newMessages as $message): ?>
    <?php $profile = $chat->getUserMessage($message['user_id']); ?>
        <?php if ($message['user_id'] == $_SESSION['user_id']): ?>
            <div id="messageBlock" class="blockRed">
                <div class="profile-chat">
                    <img class="profileSmall" src="<?php echo $profile['avatar']; ?>"/>
                    <h4 class="username"><?php echo $profile['firstname'] . ' ' . $profile['lastname']; ?></h4>
            <p class="message"><?php echo $message['message'] ?></p>
    <?php else: ?>
        <div id="messageBlock" class="blockYellow">
            <div class="profile-chat">
                <img class="profileSmall src="<?php echo $profile['avatar']; ?>"/>
                    <h4 class="username"><?php echo $profile['firstname'] . ' ' . $profile['lastname']; ?></h4>
            <p class="message"><?php echo $message['message'] ?></p>
    <?php endif; ?>
<?php endforeach; ?>

Edit in Ajax (added a few lines to find the correct 'gesprek_id' with the solution of the $_GET issue) Ajax/Jquery:

$(document).ready(function () {

function loadchat() {
    var messageList = $('#allMessages');
    var gesprekId = $('.message').attr('data-gesprekId');
setInterval(loadchat, 50000);

I hope that my question is clear.

It's also my first question asked in Stackoverflow so I'm sorry if something is not well described. (and sorry if I made some errors in English ;) ) Thank you by advance.

  • 写回答

1条回答 默认 最新

  • doulao7998636570 2017-06-06 19:55

    Change your call to


    It should start working.

    Basically, you're trying to make a GET request to newChat.php . You're trying to access the same parameter in PHP. Since, there are no GET parameters, GET is throwing a warning.

    But, in this load function. you're not sending any GET parameters. Hence, the change to gesprek_id=abc.

    About doing a polling repeatedly, You need to rewrite the setInterval function better.

    setInterval(function() {
          // Do something every 5 seconds
    }, 5000);

    I'm not sure if this is the ideal way. Something feels a little odd here. But, I would think, there will be better ways of doing this. Anyhow, I've rewritten the setInterval call for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



    • ¥15 Qt 不小心删除了自带的类,该怎么办
    • ¥15 我需要在PC端 开两个抖店工作台客户端.(语言-java)
    • ¥15 有没有哪位厉害的人可以用C#可视化呀
    • ¥15 可以帮我看看代码哪里错了吗
    • ¥15 设计一个成绩管理系统
    • ¥15 PCL注册的选点等函数如何取消注册
    • ¥15 问一下各位,为什么我用蓝牙直接发送模拟输入的数据,接收端显示乱码呢,米思齐软件上usb串口显示正常的字符串呢?
    • ¥15 Python爬虫程序
    • ¥15 crypto 这种的应该怎么找flag?
    • ¥15 代码已写好,求帮我指出错误,有偿!