0

i need a little help with one of the sections of my website which involves private messages. The page in question has 4 tabs on it, one of the tabs called inbox displays a table of all the messages received, the subject of each message has a link which can go to another page displaying the message in greater detail. However what i want is for the content in the inbox tab to be replaced by the code for displaying the message in more detail.

Here is the code for the inbox table

<div id="inbox" class="tab_content">
    <table id="mail_header">

              <tr class="mailHeader">
                <th>Message ID</th>
                <th>From</th>
                <th>Subject</th>
                <th>Content</th>
                <!--<th>Date Completed</th>-->
                <th>Date Sent</th>
                <!--<th>Refused</th>-->

                </tr>
                        <?php
                        $sql = mysql_query("SELECT * FROM user_message WHERE id=$id");
                        $rowCheck = mysql_num_rows($sql);
                        if ($rowCheck == 0) {
                           echo "You Have No Messages";
                           die;
                        }
                        else {
                           while ($query = mysql_fetch_array($sql)) {
                                    echo '<div id="message_content">';
                                  echo '<tr>';
                                  $message_id = $query['message'];
                                  $from = $query['from'];
                                  $subject = $query['subject'];
                                  $message_content = $query['message_content'];
                                  $date_sent = $query['date_sent'];
                                  $viewed = $query['viewed'];
                                  if($viewed == '0'){
                                    echo "<td><boldMail>$message_id</boldMail></td>";
                                    echo "<td><boldMail>$from</boldMail></td>";
                                    echo "<td><boldMailSubject><a href='mail.php?mail=$message_id'>$subject</a></boldMail></td>";
                                    echo "<td><boldMail><div>$message_content</div></boldMail></td>";
                                    echo "<td><boldMail>$date_sent</boldMail></td>";
                                  }
                                  else{
                                    echo "<td>$message_id</td>";  
                                    echo "<td>$from</td>";
                                    echo "<td><a href='mail.php?mail=$message_id'>$subject</a></td>";
                                    echo "<td><div>$message_content</div></td>";
                                    echo "<td>$date_sent</td>";
                                  }


                                  echo '</tr>';
                                  echo '</div>';
                           }
                        }
                        ?>
                </table>

</div>

Here is the code i want to replace the above code on clicking one of the messages

<?php
        $mail_id = "";
        $mail_id = $_GET['mail'];
        if($mail_id = ""){
            $string = get_include_contents('mailContent.php');
            echo "$string";
        }
        else{

            $mail_id = "";
            $mail_id = $_GET['mail'];

            $sql_mail = mysql_query("SELECT * FROM user_message WHERE message='$mail_id'");
            $mail_array = mysql_fetch_assoc($sql_mail);

            $message_id = $mail_array['message'];
            $from = $mail_array['from'];
            $subject = $mail_array['subject'];
            $message_content = $mail_array['message_content'];
            $date_sent = $mail_array['date_sent'];

            echo "$message_id";
            echo "$from";
            echo "$subject";
            echo "$message_content";
            echo "$date_sent";

        }
        ?>

Thanks for your ideas, even if you can tell me if i need actionscript or javascript etc... i would appreciate it.

Alistair Wise
  • 131
  • 1
  • 3
  • 7

1 Answers1

0

What you want cannot be achived by PHP alone: you want to use AJAX to load the message into an already existing part of your webpage.

A simple way of doing this would be to use jQuerys load() method

But beware: adding this kind of behaviour to your page can also break things. For example: will you still be able to link to the single messages in you system if you replace the current mechanism for displaying a message with the AJAX request?

Here's a german language article on this. (sorry, can't find an english one just now)

Also: your PHP code is suceptibel to SQL-Injection. Learn about prepared statements and use mysqli to avoid this problem.

Community
  • 1
  • 1
bjelli
  • 9,752
  • 4
  • 35
  • 50