我的网站上安装了邮件系统。对于用户收到的每条消息,我都希望有一个答复表单,该表单知道要答复发送原始消息的人。
我可以显示表格并发送消息,但是答复将发送给最后收到消息的用户,而不是发送已发布消息的用户。
HTML和Javascript:
<!–– INBOX START ––>
<div id="navitem1" onclick="navitem1(event)" class="navitem1"><i class="fa fa-envelope fa-lg" id="navicon"></i><div class="redball"><div class="message_count"></div></div><p class="text">Inbox</p>
</div>
<div id="navitem1dropdown" class="navitem1content">
<h3>Inbox</h3>
<div class="sectionheader2"></div>
<div class="inboxscroll">
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('.message_count').html('0');
function load_unseen_messages(viewmsg = '')
{
$.ajax({
url:"inbox.php",
method:"POST",
data:{viewmsg:viewmsg},
dataType:"json",
success:function(data)
{
$('.inboxscroll').html(data.messagehtml);
if(data.message_count > 0)
{
$('.message_count').html(data.message_count);
}
}
});
}
load_unseen_messages();
$(document).on('click', '.navitem1', function(){
$('.message_count').html('0');
load_unseen_messages('yes');
});
setInterval(function(){
load_unseen_messages();;
}, 5000);
});
</script>
<script>
$('#inboxForm').submit(function(){$('input[type=submit]', this).attr('disabled','disabled');});
function replyPM(){
var pmTextArea = $("#pmTextArea");
var sendName = $("#pm_send_name");
var sendID = $("#pm_send_id");
var receiveName = $("#pm_receive_name");
var receiveID = $("#pm_receive_id");
var timesent = $("#pm_timesent");
var url = "messages.php";
$.post(url,{ message: pmTextArea.val(), sendername: sendName.val(), senderid: sendID.val(), recname: receiveName.val(), recID: receiveID.val(), time: timesent.val() }, function(data){
$("#interaction").html('Message sent successfully.').show().fadeOut(5000);
document.inboxForm.pmTextArea.value='';
});
}
</script>
PHP:
<?php
session_start();
require_once 'class.channel.php';
$user_inbox = new USER();
$user_id = $_SESSION['userID'];
$user_name = $_SESSION['userName'];
if(isset($_POST["viewmsg"]))
{
if($_POST["viewmsg"] != '')
{
$stmt = $user_inbox->runQuery("UPDATE inbox SET status = 1 WHERE receive_id = ?");
$stmt->bindValue(1,$user_id);
$stmt->execute();
}
$stmt = $user_inbox->runQuery("SELECT * FROM inbox WHERE receive_id= ? ORDER BY id DESC LIMIT 50");
$stmt->bindValue(1,$user_id);
$stmt->execute();
$messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
$messagehtml = '';
if(count($messages) > 0)
{
foreach($messages as $message)
{
$messagehtml .= '
<div class="inboxsection"><h6>From: '.$message["send_name"].'</h6><h6>'.$message["timesent"].'</h6><h6>Message: </h6><img src="images/preview2.jpg" height="100%" width="100%" class="inboxvid">
<h7>'.$message["comment"].'</h7>
<div style="clear:both;"><br><button id="reply" class="button" onclick="inboxmessage(event)">Reply</button> <button id="archive" class="button">Archive</button> <button id="delete" class="button">Delete</button></div>
</div>
<div id="inboxmessage" class="messagearea">
<form action="javascript:replyPM();" name="inboxForm" id="inboxForm" method="post">
<input name="pm_send_id" id="pm_send_id" type="hidden" value="'.$user_id.'" />
<input name="pm_send_name" id="pm_send_name" type="hidden" value="'.$user_name.'" />
<input name="pm_receive_id" id="pm_receive_id" type="hidden" value="'.$message["send_id"].'" />
<input name="pm_receive_name" id="pm_receive_name" type="hidden" value="'.$message["send_name"].'" />
<input name="pm_timesent" id="pm_timesent" type="hidden" value="" />
<div id="interaction"></div>
<br>
<p style="color:#fff;">Message:</p>
<textarea name="pmTextArea" id="pmTextArea"></textarea>
<p style="color:#fff;">Attach A Video:</p>
<input name="pmSubmit" type="submit" value="Submit" />
</form>
</div>
<div class="sectionheader3"></div>
';
}
}
else
{
$messagehtml .= '<div><h2 style="color: #4b8ed3; padding: 10px;">No Messages Found<h2></div>';
}
$count = $user_inbox->runQuery("SELECT * FROM inbox WHERE receive_id= ? AND status= 0");
$count->bindValue(1,$user_id);
$count->execute();
$countresult = $count->fetchAll(PDO::FETCH_NUM);
if(count($countresult) > 0){
$message_count = count($countresult);
}
else{
$message_count = 0;
}
header('Content-type: application/json');
$inbox_array = array('messagehtml'=>$messagehtml,'message_count'=>$message_count);
echo json_encode($inbox_array);
}
?>
最佳答案
创建/插入您的回复后,请具有一个引用字段,例如parentId
,将其引用到要回复的原始邮件。
这样,如果您喜欢用户表的引用列,就可以获取用户的信息。前createdBy
我可以更新此答案,如果提供inbox
表的列列表,则可以为您提供更多详细信息。
获取已发布消息的主要ID
根据您的表设计,我会这样做:
$postedMessageId = $_GET['id'];// you can pass the id however you want.this is just a sample
INSERT INTO inbox ("parentId","comment",....) VALUES ( <postedMessageId>, <comment>,.... )
这样,您现在可以使用JOIN来使用
parentId
列引用所有答复。