0

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);
    $stm->execute();
    $res = $stm->fetchAll();
    return $res;
}

Php for Ajax

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

include_once('../classes/Db.php');
include_once('../classes/Chat.php');

session_start();

$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']; ?>"/>
                <div>
                    <h4 class="username"><?php echo $profile['firstname'] . ' ' . $profile['lastname']; ?></h4>
                </div>
            </div>
            <p class="message"><?php echo $message['message'] ?></p>
        </div>
    <?php else: ?>
        <div id="messageBlock" class="blockYellow">
            <div class="profile-chat">
                <img class="profileSmall src="<?php echo $profile['avatar']; ?>"/>
                <div>
                    <h4 class="username"><?php echo $profile['firstname'] . ' ' . $profile['lastname']; ?></h4>
                </div>
            </div>
            <p class="message"><?php echo $message['message'] ?></p>
        </div>
    <?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');
    $(messageList).load('ajax/newChat.php?gesprek_id='+gesprekId);
}
setInterval(loadchat, 50000);
loadchat();
});

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.

  • 3
    You are not setting the GET parameter whe requesting the script, it should be something like: `$(messageList).load('ajax/newChat.php?gesprek_id=xxx');` You could check if it `isset`=>`if(isset($_GET['gesprek_id'])){...}else{other logic insert here}`. P.S: `session_start()` should be the first line in your file (more: https://stackoverflow.com/questions/10542089/when-to-use-session-start) – ka_lin Jun 06 '17 at 19:53
  • "The url looks like this `chat.php?gesprek_id=1`." - According to the AJAX code you're showing us, the URL is: `'ajax/newChat.php'` – David Jun 06 '17 at 20:01
  • I see. I tried this method and it almost works! The div reloads with all the messages but after the interval period this div is empty again. The 'gesprek_id' is undefined. After a few seconds the div is again filled with all my messages etc. – Chris Oortman Jun 06 '17 at 20:23
  • I added a few lines in the Jquery file. – Chris Oortman Jun 06 '17 at 20:27

1 Answers1

1

Change your call to

$(messageList).load('ajax/newChat.php?gesprek_id=abc');

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
      loadchat();
}, 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.

Umashankar Das
  • 601
  • 4
  • 12