Javascript 如何将url id传递到不同的php页面?
现在我面临一个问题,那就是我无法获取页面的id,我正在使用不同id创建多个房间,您可以选择您想要的房间。例如,房间a=id 1和房间b=id 2 一旦你选择了url,它就会被删除Javascript 如何将url id传递到不同的php页面?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,现在我面临一个问题,那就是我无法获取页面的id,我正在使用不同id创建多个房间,您可以选择您想要的房间。例如,房间a=id 1和房间b=id 2 一旦你选择了url,它就会被删除 localhost/pme/main/chatRoom.php?chatroomID=1 localhost/pme/main/chatRoom.php?chatroomID=2 我有一个gui房间页面,我们称之为chatroom.php和多个函数文件,但我使用了 chatroomid = $_GET['chat
- localhost/pme/main/chatRoom.php?chatroomID=1
- localhost/pme/main/chatRoom.php?chatroomID=2
chatroomid = $_GET['chatroomID'];
我将此代码放在所有函数php文件中,但当我插入数据库时,值为0
- insertmessage.php-用于插入消息
- loadmessage.php-用于加载消息
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;
}
}
var chatroomid = (location.search.match(/chatroomID=(\d+)/) || [])[0];
xmlhttp.open('GET','chatNew.php?&message='+ message +"&chatroomID="+chatroomid,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",
success:function(data)
{
setValue(data.value);
},
dataType:"json",
complete:poll
});
},
30000
);
});
您是否通过AJAX调用这些文件 如果是这样,您仍然需要将参数添加到这些调用中——请记住,即使它们与原始调用加载在同一个页面中,就PHP而言,它们是完全独立的请求 例如,你会有:
xhr.open("POST","insertmessage.php?chatroomID="+roomid,true);
您可以通过在数据属性中传递该房间ID,或者通过读取JS中的查询字符串来获取该房间ID:
var roomid = (location.search.match(/chatroomID=(\d+)/) || [])[0];
if( !roomid) throw new Error("Could not find room ID.");
我个人这样做的方式是
require\u once()
依次加载最后一个文件。。虽然这可能不是最好的方法。是的,我使用ajax调用了所有函数文件,我用ajax代码编辑了我的问题。你有没有尝试过实现我给你的解决方案?我没有,我用正确的方式使用它,或者没有,我无法收到url idi也尝试过,但似乎没有工作代码=var chatroomid=(location.search.match)(/chatroomID=(\d+/)| |[])[0];xmlhttp.open('GET','chatNew.php?&message='+message+“&chatroomID=“+chatroomID,true)您的chatNew.php
文件访问$\u获得['chatroomID']
又一次了吗?您不能直接使用$chatroomID
,因为您在这个新请求中没有定义它。