Chat Room List Online Users
The chat room script below creates a list of online users to display in the chat room where they do their chatting. It also creates a dropdown list of these users so users can select a screen name of someone they'd like to chat privately with. It also updates the chatroommembers table regarding private chat status.
On to the PHP code. As usual, we start with config.php, since without it, the MySQL-based app would not be viable. You cannot relate to a db without knowing the magic words. We GET the username into $U, and the end-of-the-private-status flag into $go_priv. Then we search this user's MySQL db table data for their privacy related flag and name, and we do the same for the user you intend to have a private chat with. Next we declare a $names array, then find all screen names in the db table that are logged in and in Public mode. These are put into the array with the PHP array_push() function. Now a selectable HTML dropdown list of those Public screen names is created in an echo statement that will be sent to the chat room home page in AJAX xmlhttp.responseText as long as the user is in Public mode.
Note that the input tag has type='button', value='Submit for\nprivate chat', and onclick='private()' in it. It is not a submit type of input—it's a button, so the form action is irrelevant and unused. It runs the JavaScript function private() on the chat room home page.
If your privacy flag is 3 (meaning someone wants a private chat with you) and there is a screen name in your screen name flag, you'll see "If you would like to chat with [their name], click Private Chat. If not, click Public Chat." in the right column where the list of online users goes. If both your and your chat partner's privacy flags are set, you are in Private mode and instead of the list of Public users online, you'll see just your name and his/hers under the title: "PRIVATE Chat Room". If your $go_priv flag is 9 that means you just ended the private chat by hitting Public. If your privacy flag is 3 it means you just got asked to do a private chat but you clicked your Public button to signify NOPE. The result of this rejection is that the privacy related flags and name fields in BOTH your record and his/her record will be updated to reflect Public mode. If the other user hits the Public button while you are in private chat mode, your $priv_flag will be getting updated as a 1 from other PHP scripts because the URL query strings that update this flag will get their info from the JavaScript flags in your chat room home page flag, but the $priv_name flag will still be empty because of the other person's clicking on Public. So this will send a 14-hyphen signal through AJAX which will make your JavaScript flags get reset to Public mode. It will also set your record's privacy related flag field and privacy name field to 0 and empty, respectively.
The actual code logic whereby one user asks another if s/he wants to chat in private is handled by a combination of JavaScript flags, PHP flags, and db table field flag values. Flags get sent from one PHP script to another via the URL query string, never in the AJAX xmlhttp.responseText which would be nice to pull off—but too messy.
Save this file as logged-in.php
<?php
include_once"config.php";
$U=$_GET['u'];$go_priv=$_GET['go_priv'];
$result = mysql_query("SELECT priv_if,priv_who,screen_name FROM chatroommembers WHERE username='$U'") or die(mysql_error());
$row = mysql_fetch_array($result);$priv_flag=$row['priv_if'];$priv_name=$row['priv_who'];$sn=$row['screen_name'];
$result = mysql_query("SELECT priv_if,priv_who FROM chatroommembers WHERE screen_name='$priv_name'") or die(mysql_error());
$row = mysql_fetch_array($result);$priv_flag2=$row['priv_if'];$priv_name2=$row['priv_who'];
$names=array();
if ($priv_flag==0){
echo "<center><b>Online Now</B></center>";
$result = mysql_query("SELECT screen_name FROM chatroommembers WHERE logged_in <> 0 AND priv_if = '0' order by screen_name") or die(mysql_error());
while($row = mysql_fetch_array($result)) {
array_push ($names, $row['screen_name']);
echo $row['screen_name']."<BR>";}
$num=count($names);
echo "<form name='form' method='post' action=' '><hr><table><tr><td>(Private)</td><td><select name='priv'>";
for ($i=0;$i<$num;$i++) {
echo "<option value='".$names[$i]."'>".$names[$i]."</option>";}
echo "</select></td></tr><tr><td colspan=2><center> <input name='send_priv' type='button' value='Submit for\nprivate chat' onclick='private()'></center></td></tr></table></form>";
}
if ($priv_flag==3 && $priv_name<>""){echo "<div style='background-color:#88f;font-size:18px'>If you would like to chat with ".$priv_name.", click Private Chat. If not, click Public Chat.</div>";}
if ($priv_flag==1 && $priv_flag2==1){echo "<b>PRIVATE Chat Room</b><BR>".$sn."<BR>".$priv_name."<BR>(private chat)";}
if($go_priv==9 && $priv_flag==3){$priv_flag=0;mysql_query("UPDATE chatroommembers SET priv_if = '0',priv_who = '' WHERE username='$U' OR screen_name='$priv_name'") or die('Error ,saving failed');}
if ($priv_flag==1 && $priv_name==""){echo "--------------";mysql_query("UPDATE chatroommembers SET priv_if = '0',priv_who = '' WHERE username='$U'") or die('Error ,saving failed');}
mysql_close();
?>
<BR>