Hi!

I got this messaging system on my website up and running now.
But there is something i need to do when the user click the message they got.
All the new messages are flagged as New! when they get the message.
When they click on it, i got this jQuery script to just slide the message down, for a clean / smooth look. Again, all of this is happening at the same site (inbox.php).

I also need to flag the messages as Old / Read when they click on the message and it slides down. Like a mysql query onClick.

I read some Ajax and jQuery posts and my code is not working. I get an error saying this:

Parse error: syntax error, unexpected T_LNUMBER in blabla...

My code til now looks like this:

<? ob_start(); ?>
<?php session_start(); if ($_SESSION['username']) {} else {header("location:mysite-.com"); exit();}?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Underbyen</title>
    <link rel="shortcut icon" href="resources/favicon.ico">
    <link rel="stylesheet" type="text/css" href="resources/style.css" />
	<script src="http://code.jquery.com/jquery-latest.js"></script>
    <script> $(document).ready(function() { $(".showsub").click(function() { $(".sub").animate({height: 'toggle' }); }); }); </script>
    <script> $(document).ready(function() { $(".showsub2").click(function() { $(".sub2").animate({height: 'toggle' }); }); }); </script>
    <script> $(document).ready(function() { $("#watcher").click(function() { $("#footer").animate({height: 'toggle' }); }); }); </script>
</head>

<body>
    
    <div id="header">
    <?php include 'header.php'; ?>
    </div>
    
    <div id="wrapper">
    <table>
    <tr>
    	<td width="250" valign="top">
            <?php
			if ($_SESSION['username']) 
            {
				include 'usermenu.php';	
			}
			?>
        </td>
		<td width="600" valign="top">
			<?php
			if ($_SESSION['username'])
			{
				include 'db_connect.php';
				$username = $_SESSION['username'];
				
				//Get the messages
				$get_messages = mysql_query("SELECT id FROM messages WHERE to_user='$username' ORDER BY id DESC") or die(mysql_error());
				$get_messages2 = mysql_query("SELECT * FROM messages WHERE to_user='$username' ORDER BY id DESC") or die(mysql_error());
				$num_messages = mysql_num_rows($get_messages);
				

				
				//Display the message title with the link to the content
				echo '<div id="messages" class="box_square"><div align="center"><img class="label" src="resources/inboks.png" width="129" height="27" /></div><ul class="msgs">';
				$i = 0;
					for ($count = 1; $count <= $num_messages; $count++)
					{
						$row = mysql_fetch_array($get_messages2);
						$message_read = $row['message_read'];
						$senders_username = $row['from_user'];
						$senders_id = $row['from_id'];
						$message_id = $row['id'];
						
						//Script to show / hide message
						echo '<script> $(document).ready(function() { $(".click_'.$i.'").live("click", function() { $("#msg_case_'.$i.'").animate({height: "toggle" }); }); }); </script>';
						echo '<script> $(document).ready(function() { $(".click_'.$i.'").click(function() { $("#msg_case_'.$i.'").load("message_update.php"); }); }); </script>';
						
						//If the message is new, show it, else just show the title
						if ($message_read == 0)
						{
							echo '<div id="new_msg" class="message_row box_square" style="background-color:#101010;"><a class="click_'.$i.'">'.$row['title'].'</a><div class="status" style="color:#186b86;">Ny</div></div><br>';
							echo '
							<div id="msg_case_'.$i.'" class="msg_case box_newest" style="padding:10px;">
							Fra: <a href="mysite.com/user.php?id='.$senders_id.'">'.$row['from_user'].'</a><br><br>
							'.$row['body'].'<br><br>
							'.$row['message_date'].'   '.$row['message_time'].'
							</div>
							';
						}
						
						else
						{	
							echo '<div id="msg" class="message_row box_square" style="background-color:#101010;"><a class="click_'.$i.'">'.$row['title'].'</a><div class="status">Lest</div></div><br>';
							echo '
							<div id="msg_case_'.$i.'" class="msg_case box_newest" style="padding:10px;">
							Fra: <a href="mysite.com/user.php?id='.$senders_id.'">'.$row['from_user'].'</a><br><br>
							'.$row['body'].'<br><br>
							'.$row['message_date'].'   '.$row['message_time'].'
							</div>
							';
						}
						$i++;
					}

				echo '</ul>';
			}
			?>
            </td>
    </tr>
    </table>
    </div>
   
    <div id="footer">
    <?php include 'footer.php'; ?>
    </div>


</body>
</html>
<? ob_flush(); ?>

Also, my message_update.php looks like this:

<?php
include 'db_connect.php';

$sql = "UPDATE messages SET message_read='1' WHERE message_id='$message_id' AND to_user='$username'";
mysql_query($sql);
?>

Can anyone help me out?
Thx in advance!

in which line you got this error?

Parse error: syntax error, unexpected T_LNUMBER in blabla...

Line 4 in the message_update.php script.

No php syntax error in line 4.
I think your message_read or message_id field may be integer datatype in database. If so remove the single quotes in the value of the field like this

$sql = "UPDATE messages SET message_read=1 WHERE message_id=$message_id AND to_user='$username'";

But it may not be syntax error.

That removed the syntax error, but the box where the message slides in dissapeard on click, and it does not change the 0 to 1 in the database. It there any other way to run a query onClick?

I think AJAX is the easy thing than jquery for this operation...

Hmm okay, is there any good explanations or tutorials for what im trying to do?

Where do $message_id and $username get there value ?
if not in db_connect.php then they don't exist

I thought that they existed from the inbox.php since the message_update.php is ran inside of inbox.php. I can try to put the variables in the message_update.php and see what happens. :)

Nothing is happening when i set the variables in the message_update.php either. So i will go for the AJAX solution, thanks for all help! :)

That would be true if you used include or require ,
but you use an javascript function to load it seperetly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.