Quatban Taco Quatban Taco - 1 month ago 4x
jQuery Question

Private Message Using JQuery and PHP

I am currently working on a Chat Application and ran into a slight hiccup, I want a message that is addressed to a user to only be visible to the user and the sender.

var username = '<?=$_SESSION['username'];?>'.toUpperCase();
var msgln = $(this);
$$select = msgln.find('.messagelinebox');
$message = $$select.text();
if($message.indexOf('@') !== -1){
var $product = $message.substr($message.indexOf("@") + 1);
$$product = $product.substring(0,7).toUpperCase();
var receiver = $$product;
if(username = receiver){
}else if(receiver = username){
}else if(receiver != username){

Any help will be appreciated.


As pointed out by everyone, you're going about things the wrong way.

However, there are some serious problems with your if() statement. This code:

if(username = receiver) {
    /* code */

is exactly equivalent to the following:

username = receiver;
if(username) {
    /* code */

If you want to test if two things are equal, use ==, not =. Also, if a==b, then there is no need to test for b==a. Your entire if ... else structure can be reduced to the following:

if (username == receiver) {
else {

Your code only checks for messages that were sent to the current user. It doesn't check for messages sent by the current user. That's another thing you will have to fix.

But really, do this all on the server. Not in client-side Javascript.