我如何将我的聊天室ID传递给我的ajax电话?

时间:2016-05-28 10:11:47

标签: php jquery ajax

我想在用户选择不同房间时调用id。不同的id有不同的身份,所以我怎么让ajax知道我进入哪个房间?

我已经使用ajax调用两个函数php

  • chatNew.php用于插入消息
  • chatMessage.php for load message

我使用了$chatroomID =$_GET['chatroomID']; 调用id但似乎没有工作并得到错误

-localhost / pme / main / chatroom.php?chatroomID = 1<<我如何得到chatroomID

这里是chatrooms.php我用sql语句从数据库中选出所有的聊天室

 <td>
        <a onclick="poll" href="chatroom.php?chatroomID=<?php echo $row['id'];?>">
            <div>
                <p>
                    <?php echo ($row['name']); ?>
                </p>
            </div>
        </a>
    </td>

这是我的js文件

function submitChat(){
        var message = chatroom.message.value;
    if(chatroom.message.value == ''){
        alret('You didnt input any message');
        return;
    }

    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
            document.getElementById('inner').innerHTML = xmlhttp.responseText;
        }
    }

    xmlhttp.open('GET','chatNew.php?&message='+message,true);
    xmlhttp.send();
    $('.chatroom-message-container').scrollTop($('.chatroom-message-container').get(0).scrollHeight);
}

$(document).ready(function(e){

    $.ajaxSetup({cache:false});
    setInterval(function(){$('#inner').load('chatMessage.php');}, 2000);

    $(".sendmessage-btn").click(function(){
        $("#area-message").val('');     
    });  
});

(function poll() {
    setTimeout(function(){
        $.ajax({
            url:"chatRoom.php?chatroomID=id", 
            success:function(data)
            {
                setValue(data.value);
            },
            dataType:"json",
            complete:poll
        });
    },
    30000
            );  
});

3 个答案:

答案 0 :(得分:2)

您可以使用Ajax数据选项发送变量,您可以通过PHP GET获取chatRoom.php。

(function poll() {
setTimeout(function(){
    $.ajax({
        url:"chatRoom.php", 
        data:{chatroomID:id},
        success:function(data)
        {
            setValue(data.value);
        },
        dataType:"json",
        complete:poll
    });
},30000);  
});

答案 1 :(得分:1)

您可以使用提交按钮(如此

)将参数传递给轮询功能
<button onclick="poll('<?php echo $id; ?>')">send</button>

(function poll(id) {
        setTimeout(function(){
            $.ajax({
                url:"chatRoom.php?chatroomID="+id, 
                success:function(data)
                {
                    setValue(data.value);
                },
                dataType:"json",
                complete:poll
            });
        },
        30000
        );  
 });

答案 2 :(得分:0)

最简单的解决方案是改变..

xmlhttp.open('GET','chatNew.php?&message='+message,true);

..包括chatroomID

xmlhttp.open('GET','chatNew.php?message=' + message + '&chatroomID=<?php echo $row['id'];?>', true);
相关问题