0

Sorry, may i know what is the problem?

load_data = {'fetch':1};
window.setInterval(function(){
 $.post('shout.php', load_data,  function(data) {
    $('.message_box').html(data);
    var scrolltoh = $('.message_box')[0].scrollHeight;
    $('.message_box').scrollTop(scrolltoh);
 });
}, 1000);

$("#shout_message").keypress(function(evt) {
        if(evt.which == 13) {
                var iusername = $('#shout_username').val();
                var imessage = $('#shout_message').val();
                var roomID = $('#room_id').val();
                post_data = {'username':iusername, 'message':imessage, 'roomID':roomID};

                //send data to "shout.php" using jQuery $.post()
                $.post('shout.php', post_data, function(data) {

                    //append data into messagebox with jQuery fade effect!
                    $(data).hide().appendTo('.message_box').fadeIn();

                    //keep scrolled to bottom of chat!
                    var scrolltoh = $('.message_box')[0].scrollHeight;
                    $('.message_box').scrollTop(scrolltoh);

                    //reset value of message box
                    $('#shout_message').val('');

                }).fail(function(err) { 

                //alert HTTP server error
                alert(err.statusText); 
                });
            }
    });

The problem come to this page, shout.php

<?php

if($_POST)
{
    //check if its an ajax request, exit if not
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
        die();
    } 


    if(isset($_POST["message"]) &&  strlen($_POST["message"])>0)
    {

        $username = $_POST["username"];
        $message = $_POST["message"];
        $room_id = $_POST["roomID"];
        $user_ip = $_SERVER['REMOTE_ADDR'];


        if(mysqli_query($sql_con,"INSERT INTO shout_box(user, message, ip_address, room_id) value('$username','$message','$user_ip','$room_id')"))
        {
            $msg_time = date('h:i A M d',time()); // current time
            echo '<div class="shout_msg"><time>'.$msg_time.'</time><span class="username">'.$username.'</span><span class="message">'.$message.'</span></div>';
        }

        mysqli_query($sql_con,"DELETE FROM shout_box WHERE id NOT IN (SELECT * FROM (SELECT id FROM shout_box ORDER BY id DESC LIMIT 0, 10) as sb)");
    }

    else if($_POST["fetch"]==1)
    {
        $room_id = $_POST["roomID"];  //Not working here.
        $results = mysqli_query($sql_con,"SELECT user, message, date_time FROM (select * from shout_box ORDER BY id DESC LIMIT 10) shout_box ORDER BY shout_box.id ASC");
        while($row = mysqli_fetch_array($results))
        {
            $msg_time = date('h:i A M d',strtotime($row["date_time"])); //message posted time
            echo '<div class="shout_msg"><time>'.$msg_time.'</time><span class="username">'.$row["user"].'</span> <span class="message">'.$row["message"].'</span></div>';
        }
    }
    else
    {
        header('HTTP/1.1 500 Are you kiddin me?');
        exit();
    }
}

It show the message
Undefined index: roomID in C:\xampp\htdocs\Web\chat\shout.php on line 31

It does work in line 16. What is the problem? thanks for advance

Edited by Lau_1

1
Contributor
1
Reply
9
Views
2 Years
Discussion Span
Last Post by Lau_1
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.