我曾尝试将用户名转换为字符串,并清除语法错误但无济于事。
<?php
foreach($arrlist as $farr){
$onlineusersquery = mysqli_query($con,"SELECT * FROM users WHERE
username = '$farr' AND online = '2'");
$onlineusers = mysqli_fetch_assoc($onlineusersquery);
if(in_array($farr,$onlineusers)){
$recipient = (string)$farr;
print "<span class=openclick style=cursor:pointer;margin-
left:2vmax;position:relative; onclick=friendinteract(".$num.")>".$farr."
</span></br>";
print"<div class=friendinteract>";
print "<span class=message onclick=message(".$num.") style=margin-
left:3vmax;cursor:pointer;position:relative;color:#fffff1;>Message</span>
</br>";
print "<span class=chat onclick=chat(".$recipient.") style=margin-
left:3vmax;cursor:pointer;position:relative;color:#fffff1;>Chat</span>";
print"</div>";
$num++;
}
}
?>
<script type="text/javascript">
function chat(recipient){
var username = recipient;
$.post("chat.php", {usernumber: username}, function(result){
console.log(result)
});
}
</script>
我希望将变量发送到函数,以便可以发布该变量,但是我收到未捕获的ReferenceError:未定义“用户名”
答案 0 :(得分:-1)
如果这不起作用,您可以使用jQuery
用以下内容替换范围:
"<span class='chat' data-user='".$recipient."' style='margin- left:3vmax;cursor:pointer;position:relative;color:#fffff1;'>Chat</span>";
在jquery中使用
$('.chat').on('click',function(){
var username= $(this).attr(data-user);
$.post("chat.php", {usernumber: username}, function(result){
console.log(result)
});
});