add 'reply' function to private messages
Moderator: General Moderators
add 'reply' function to private messages
Currently I coded a private messaging system, where it sends a user a message, and that user receives an alert there is a message and gets led to it. However, in my excitement I forgot to add a reply button (I just have a 'send new message' button) and am confused how to go about it so that user#1's message (that was sent to user#2) is still 'saved' when user#2 replies. So it'd be like:
Message #1:
"hi!"
User #2 hits reply button, types "Hello", user #1 would receive the message and it would look like this:
"Hello
- - - - - - - - -
hi!"
Does anyone know of any good tutorials I could look at that will explain this?
Message #1:
"hi!"
User #2 hits reply button, types "Hello", user #1 would receive the message and it would look like this:
"Hello
- - - - - - - - -
hi!"
Does anyone know of any good tutorials I could look at that will explain this?
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: add 'reply' function to private messages
What does the structure of the 'message' table in your database look like
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: add 'reply' function to private messages
Code: Select all
TABLE `messages` (
`message_id` int(11) NOT NULL auto_increment,
`from_user` varchar(65) character set latin1 collate latin1_general_ci NOT NULL,
`to_user` varchar(65) character set latin1 collate latin1_general_ci NOT NULL,
`message_title` varchar(65) NOT NULL,
`message_contents` longtext NOT NULL,
`message_read` int(11) NOT NULL default '0',
PRIMARY KEY (`message_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=21;- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: add 'reply' function to private messages
I'm not sure on the details but i would say you could use the message_id field to connect message/s that are replies; Below is the logic behind my thinking
1. User 1 sends a message; a message id is generated
2. User 2 clicks on the reply button and types a new message. The message id from User 1's message has to be used again so it could be passed along in the query string or as a hidden field. When the text area is created where message text is placed use a query to select all message text where message_id is equaled to the value passed along (via $_POST or $_GET)
Hth
1. User 1 sends a message; a message id is generated
2. User 2 clicks on the reply button and types a new message. The message id from User 1's message has to be used again so it could be passed along in the query string or as a hidden field. When the text area is created where message text is placed use a query to select all message text where message_id is equaled to the value passed along (via $_POST or $_GET)
Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: add 'reply' function to private messages
What social_experiment has suggested is the way to go with this one.
If user x creates message, user y replies, the original message_contents is grabbed from user x message and displayed on screen in textarea to allow user y to change contents as required. That message is sent and saved to database. Then user x replies again, but this time you grab the last id of the user y message and pull the contents from message_contents field and display in textarea etc etc.
This would allow each user to change the contents of each message before sending. If you really wanted to find out how all the messages were related in someway, you could save the message_id in its own reply_id field in database, then you could run query to see how many replies were made on that original message etc. The limits are endless, but the relationship must be established first between messages.
You would need to either send the message_id from the original message when the reply button is used ($_POST or $_GET) so that your new script has the id. I would suggest the $_POST option so you do not have visable query strings in your urls etc because the $_GET would mean a link like so "reply.php?id=1".
I would personally depending on your setup, save the last viewed message id as a $_SESSION['last_message_id'] and then if you click a reply link instead of a submit form button, the id is taken from the $_SESSION and not directly out of the link.
Either way, you have to get that id to the next script to be able to create that relationship inorder to reply with the original message. Some programs give option to include original message as tick box but as default it is not included. This would save process load on servers etc as it becomes the users choice if they want to include the original message with the reply.
It all depends on what type of system you are coding and what requirements you have.
Best wishes
If user x creates message, user y replies, the original message_contents is grabbed from user x message and displayed on screen in textarea to allow user y to change contents as required. That message is sent and saved to database. Then user x replies again, but this time you grab the last id of the user y message and pull the contents from message_contents field and display in textarea etc etc.
This would allow each user to change the contents of each message before sending. If you really wanted to find out how all the messages were related in someway, you could save the message_id in its own reply_id field in database, then you could run query to see how many replies were made on that original message etc. The limits are endless, but the relationship must be established first between messages.
You would need to either send the message_id from the original message when the reply button is used ($_POST or $_GET) so that your new script has the id. I would suggest the $_POST option so you do not have visable query strings in your urls etc because the $_GET would mean a link like so "reply.php?id=1".
I would personally depending on your setup, save the last viewed message id as a $_SESSION['last_message_id'] and then if you click a reply link instead of a submit form button, the id is taken from the $_SESSION and not directly out of the link.
Either way, you have to get that id to the next script to be able to create that relationship inorder to reply with the original message. Some programs give option to include original message as tick box but as default it is not included. This would save process load on servers etc as it becomes the users choice if they want to include the original message with the reply.
It all depends on what type of system you are coding and what requirements you have.
Best wishes
Re: add 'reply' function to private messages
Thanks so much, social_experiment and phphelpme! This makes a lot of sense, I'm starting to attempt to code replies. I like the tickbox option, phphelpme. How would I incorporate an html tickbox in to my code? I've only ever used HTML forms and used the $_POST relationship. Would it be something like:
Also would last_message_id need to be added to the mySQL messages table, or could this simply be a made up variable I declare when the session starts?
Code: Select all
<form action="reply.php" method="post">
Include original message?
<input type="checkbox" name="formOriginal" value="Yes" />
<input type="submit" name="formSubmit" value="Submit" />
</form>
<?php
if(isset($_POST['formOriginal']) &&
$_POST['formOriginal'] == 'Yes')
{
echo ' . $_SESSION['last_message_id'] . ';
}
else
{
echo ' ';
}
?>
Re: add 'reply' function to private messages
It would first depend on your current system design. Do you already use Sessions or are you using some other form of activity?
If you are familiar with the $_POST SUBMIT option then keep using this and just add the message_id that the user is replying as a hidden field so your reply script knows to grab the last contents from the corrent message and give option to edit it etc.
Regards the tickbox, it would depend on whether you want the user to be able to edit the previous content before the message is saved or just type their message and have the original message attached after it when they click send etc.
The tickbox was just an idea that I have come accross, but I personally display the last message content for the user to edit if need be or even remove then add their content and send.
Last message id would not need to be added to the database at all unless you are going to try and run a query to say count() the amount of replies a particular message has had. (creating a relationship betweent the messages) Almost like counting the comment posts on a particular comment thread.
If you are wanting to incorporate the tickbox scenario then you have a couple of choices to choose from:
Allow the box to be ticked which would display the last message content recieved by using javascript again giving user option to change it
Allow the box to be ticked which would just add the last message content recieved to the end of the message etc not allowing the user to change it
or just simply grab the details of the last message content and display in textarea for user to do as they deem fit without the tickbox scenario.
I must say it is becoming the standard that the previous message is displayed in the reply for the user to have the choice to either send as is along with their reply or change it if required because they are sending the message to another person etc.
You need to decide which option and scenario you are wanting which best suits your development needs.
The above little code is how you would normally complete that task by checking to see if the form had been submitted first of all then also checking to see if the value existed along with the submission. But you would also need to have the message_id submitted along with it as a hidden field so your reply script nows which previous message contents to display to the user.
So:
So when you are displaying the message to begin with to the user y (for example) you would have to have grabbed the message_id of user x message and save to string $last_message_id etc to forward to your new script for the reply. Your new script would then pull the email to details, and using the $_POST['last_message_id'] run q query on the database to extract the message and display in a textarea box for the user to do as they please with it.
Just confirm a few things first before you go ahead and change the scripts etc
Best wishes
If you are familiar with the $_POST SUBMIT option then keep using this and just add the message_id that the user is replying as a hidden field so your reply script knows to grab the last contents from the corrent message and give option to edit it etc.
Regards the tickbox, it would depend on whether you want the user to be able to edit the previous content before the message is saved or just type their message and have the original message attached after it when they click send etc.
The tickbox was just an idea that I have come accross, but I personally display the last message content for the user to edit if need be or even remove then add their content and send.
Last message id would not need to be added to the database at all unless you are going to try and run a query to say count() the amount of replies a particular message has had. (creating a relationship betweent the messages) Almost like counting the comment posts on a particular comment thread.
If you are wanting to incorporate the tickbox scenario then you have a couple of choices to choose from:
Allow the box to be ticked which would display the last message content recieved by using javascript again giving user option to change it
Allow the box to be ticked which would just add the last message content recieved to the end of the message etc not allowing the user to change it
or just simply grab the details of the last message content and display in textarea for user to do as they deem fit without the tickbox scenario.
I must say it is becoming the standard that the previous message is displayed in the reply for the user to have the choice to either send as is along with their reply or change it if required because they are sending the message to another person etc.
You need to decide which option and scenario you are wanting which best suits your development needs.
Code: Select all
if(isset($_POST['formSubmit']) {
if ($_POST['formOriginal'] == 'Yes') { // GRAB PREVIOUS MESSAGE CONTENT TO DISPLAY USING $_POST['last_message_id'] VIA SQL QUERY
}
else { // DO NOT DISPLAY PREVIOUS MESSAGE FOR USER
}
}
So:
Code: Select all
<form action = "reply.php" method = "POST">
<input type = "hidden" name = "last_message_id" value = "<php echo $last_message_id; ?>" />
<input type = "checkbox" name = "formOriginal" value = "y" />
<input type = "submit" name = "formSubmit" value = "Reply" />
</form>
Just confirm a few things first before you go ahead and change the scripts etc
Best wishes
Re: add 'reply' function to private messages
I use the $_POST submit method of messages with sessions, which grabs the current user's ID#, and in fact already have that in place, so glad that this will fit in well with the reply option!
How would I go about including the original message automatically, but if the tickbox is checked it won't? I'd need it so that the original message can not be altered. My site consists of a broad span of ages, from 13 year olds to 50 year olds and when we get reports about harassment or something over messages we have to look in to it. Screenshots of the message conversation is essential so it'd be nice to know the replier can't tamper with the original message they received.
So I would add an extra button there (reply_message.php) which grabs the reader's ID#, similar to how my new message works already? And then on the reply php I should include the tick box? Or should I include the tickbox on the read_message.php since the last_message_id session info would be there? Here's my read_message.php
and here's my new_message.php page:
Whenever a user hits 'send new message' I run it through the message check, I'm assuming I do that with the reply too right?
Here's my current messagecheck:
How would I go about including the original message automatically, but if the tickbox is checked it won't? I'd need it so that the original message can not be altered. My site consists of a broad span of ages, from 13 year olds to 50 year olds and when we get reports about harassment or something over messages we have to look in to it. Screenshots of the message conversation is essential so it'd be nice to know the replier can't tamper with the original message they received.
So I would add an extra button there (reply_message.php) which grabs the reader's ID#, similar to how my new message works already? And then on the reply php I should include the tick box? Or should I include the tickbox on the read_message.php since the last_message_id session info would be there? Here's my read_message.php
Code: Select all
<?php
include('config.php');
include('date.php');
$userfinal = $_SESSION['id'];
$messageid = $_GET['messageid'];
$message = mysql_query("SELECT * FROM messages WHERE message_id = '$messageid' AND to_user = '$userfinal'");
$message = mysql_fetch_assoc($message);
echo "<b>Title:</b>
".$message['message_title']."<br>";
echo "<b>From:</b>
".$message['from_user']."<br><br>";
echo "<b>Message:</b>
<br>".$message['message_contents']."<br>";
echo '<form name="backfrm" method="post" action="inbox.php">';
echo '<input type="submit" value="Back to Inbox">';
echo '</form>';
$q="UPDATE messages SET message_read='1' WHERE message_id = '$messageid' AND to_user = '$userfinal'";
mysql_query($q);
?>
<?php
include('footer.php');
?>Code: Select all
<?php
include('config.php');
include('date.php');
$userfinal = $_SESSION['id'];
$user = $userfinal;
?>
<form name="message" action="messageck.php"
method="post">
Subject: <input type="text" name="message_title"> <br />
To: <input type="text" name="message_to" placeholder="Player ID#"><br />
Message: <br />
<textarea rows="20" cols="50" name="message_content">
</textarea>
<?php
echo '<br /><input type="hidden" name="message_from"
value="'.$user.'"><br />';
?>
<input type="submit" value="Submit">
</form>
Here's my current messagecheck:
Code: Select all
<?php
include('config.php');
include('date.php');
$title = $_POST['message_title'];
$to = $_POST['message_to'];
$content = $_POST['message_content'];
$from = $_POST['message_from'];
$ck_reciever = "SELECT id FROM users WHERE id = '".$to."'";
if( mysql_num_rows( mysql_query( $ck_reciever ) ) == 0 ){
die("The user you are trying to contact doesn't exist. Please go back and try again.<br />
<form name=\"back\" action=\"new_message.php\"
method=\"post\">
<input type=\"submit\" value=\"Try Again\">
</form>
");
}
elseif(strlen($content) < 1){
die("You can't send an empty message!<br>
<form name=\"back\" action=\"new_message.php\"
method=\"post\">
<input type=\"submit\" value=\"Try Again\">
</form>
");
}
elseif(strlen($title) < 1){
die("You must have a Title!<br>
<form name=\"back\" action=\"new_message.php\"
method=\"post\">
<input type=\"submit\" value=\"Try Again\">
</form>
");
}else{
mysql_query("INSERT INTO messages (from_user, to_user, message_title, message_contents) VALUES ('$from','$to','$title','$content')") or die("Error: ".mysql_error());
echo "The Message Was Successfully Sent!";
?>
<form name="back" action="inbox.php"
method="post">
<input type="submit" value="Back to The Inbox">
</form>
<?php
}
include('footer.php');
?>
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: add 'reply' function to private messages
Place the original message/s outside the text area; a div or p tag should suffice. It's present but not editabledyr wrote: I'd need it so that the original message can not be altered.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: add 'reply' function to private messages
Yes, Social said the right idea about including it within the <div> or <p> tags or whatever tag you may have assigned in your CSS if you do not want it editable.
If you check the messages as standard anyway you would also need to check the reply messages just as they were new messages. it makes no difference because you should still perform the same checks.
The question you need to ask is if you need the original message included in the reply and can not be changed then why would you use a tickbox scenario? are you still wanting to give the user the option to include the original message or not?
If you are concerned about screenshots and data being manipulated, then surely you would be asking for a screen shot of the inbox message only not the outbox or sent message. So on your screenshot you would have for your inbox a screen that can not be altered regarding the text, and on reply or new you would have text field so you would know that they have possibly changed the text.
You could even include a secret field in your database that gets the original message auto saved with it even if the user does not want the message to be sent to that user. That way when you check the messages if a user has stated they are being harrassed etc and they take screen shot of message, you can check the database for the additional original message field. It all depends whether or not you allow the user to delete messages and if you keep a secure backup of all communications on your servers etc.
If the user took a screenshot but then deleted the email on your systems, have you a way to recall that information (backup maybe/archive etc) which you can compare to?
In your new_message.php file what is the point in this:
You are assigning one value to two string variables so why not just use $userfinal and scrap the $user ?
I would just suggest that after reading your code you have lots of forms that users must click to go back here there and everywhere, even when message is sent successfully. I would personally have an include file for say notifications, then I would set a variable session such as $_SESSION['notif'] = "x"; (where x would be the id of your notification message) then you include this file in your pages, that way you could redirect the user back to inbox etc when message is submitted successfully and display the notification because you set the session variable to a particular number which an if statement in the include could check for and display a message and right after clear the session variable so it does not show again even on page refresh etc.
The if statement could be something like:
All you would do is save your session variable as $notif at the beginning of every file etc or just call it directly from the session. I would as a user prefer this to happen then having to keep clicking this button and that button but that is just my personal preferrence.
Best wishes
If you check the messages as standard anyway you would also need to check the reply messages just as they were new messages. it makes no difference because you should still perform the same checks.
The question you need to ask is if you need the original message included in the reply and can not be changed then why would you use a tickbox scenario? are you still wanting to give the user the option to include the original message or not?
If you are concerned about screenshots and data being manipulated, then surely you would be asking for a screen shot of the inbox message only not the outbox or sent message. So on your screenshot you would have for your inbox a screen that can not be altered regarding the text, and on reply or new you would have text field so you would know that they have possibly changed the text.
You could even include a secret field in your database that gets the original message auto saved with it even if the user does not want the message to be sent to that user. That way when you check the messages if a user has stated they are being harrassed etc and they take screen shot of message, you can check the database for the additional original message field. It all depends whether or not you allow the user to delete messages and if you keep a secure backup of all communications on your servers etc.
If the user took a screenshot but then deleted the email on your systems, have you a way to recall that information (backup maybe/archive etc) which you can compare to?
In your new_message.php file what is the point in this:
Code: Select all
$userfinal = $_SESSION['id'];
$user = $userfinal;
I would just suggest that after reading your code you have lots of forms that users must click to go back here there and everywhere, even when message is sent successfully. I would personally have an include file for say notifications, then I would set a variable session such as $_SESSION['notif'] = "x"; (where x would be the id of your notification message) then you include this file in your pages, that way you could redirect the user back to inbox etc when message is submitted successfully and display the notification because you set the session variable to a particular number which an if statement in the include could check for and display a message and right after clear the session variable so it does not show again even on page refresh etc.
The if statement could be something like:
Code: Select all
if ($notif == "1") { echo "<div class=\"notification success\">Message sent successfully</div>"; $_SESSION['notif'] = ""; }
Best wishes