最近我试图建立一个对话列表,左边会显示一个对话列表,用户可以点击他们想要看到的对话列表,因此,我写了以下内容:
AJAX:
<script type="text/javascript">
(document).on("click", "#leftconversation", function(){
var cid = $(this).attr('class'); //you are getting the user id here
var request = $.ajax({
url: "conversation.php",
type: "POST",
data: { cid: cid },
beforeSend: function(){
self.html("Loading please wait...");
}
});
//WHEN SUCCESS
request.success(function( data ) {
$("#right").html(data); //this line will replace the right with echoed content from php file
});
});
</script>
这里的代码基本上等待用户选择div(id左对话),当用户选择时,获取div的类,为不同的对话分配一个唯一的变量,之后将cid发送到conversation.php处理数据库中数据的页面,并回显hj,ajax将div附加到右侧。
这是我的php代码:
<?php
session_start();
include_once("config.php");
if (isset($_POST['cid'])){
$query= mysqli_query($mysqli,"SELECT R.cr_id,R.time,R.reply,U.Memberid,U.Username,U.Email, U.Picture FROM members U, conversation_reply R WHERE R.user_id_fk=U.Memberid and R.c_id_fk='".$_POST['cid']."' ORDER BY R.cr_id ASC LIMIT 20") or die(mysqli_error($mysqli));
while($row=mysqli_fetch_array($query))
{
$cr_id=$row['cr_id'];
$time=$row['time'];
$reply=$row['reply'];
$user_id=$row['Memberid'];
$username=$row['Username'];
$email=$row['Email'];
$picture=$row['Picture'];
echo '<div class=commments>';
echo '<div id=left2>';
echo '<img height="70px" width="55px" src="profileimage/'.$picture.'">';
//echo $username;
echo '</div>';
echo '<div id=right2>';
echo $reply;
echo '</div>';
echo '</div> <div style="clear:both"></div>';
}}else {
echo 'Sorry it failed';
}
?>
此处的代码获取所有对话回复并将其回显给ajax以便将其打印出来,但是,此脚本不起作用并且单击div甚至不执行任何操作。我没有设置数据类型,所以我不确定是否会导致错误,但我不这么认为。我有错误的步骤吗?
谢谢!