PHP Coding problem(Newbie)

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
flashery
Forum Newbie
Posts: 1
Joined: Mon Aug 08, 2011 7:33 pm

PHP Coding problem(Newbie)

Post by flashery »

Postby flashery » Mon Aug 08, 2011 9:39 am
I want to know how can I manipulate this problem.

I am making a chat app from scratch and what I want is to list a user on the div if he/she is on the chat page (not just online) and I succeeded in doing it. But the problem is when the user is not on the chat page want the user to be deleted on the list and also if the user came back to the chat page he also be added again on the list.

Here is what I have done

I created a database for chat user with the field name id username and status
I created this class to manipulate the listing and deleting of the user

Code: Select all

<?php
class Chat_User{

function user_left($user){
$query = mysql_query("UPDATE chat_users SET status = 0 WHERE username = '$user'");
if(!$query){
die(mysql_error());
}
}

function user_enter($user){
$query = mysql_query("UPDATE chat_users SET status = 1 WHERE username = '$user'");
if(!$query){
die(mysql_error());
}
}
function list_users(){

$query = mysql_query("SELECT * FROM chat_users WHERE status = 1");
if(!$query){
die(mysql_error());
}

$i = 0;
while($i < mysql_num_rows($query)){

$username = mysql_result($query, $i, "username");
echo $username."<br/>";
$i++;
}
}
}

?>
I call this class on my chat page using this

Code: Select all

require("chat_module/Chat_User.class.php");

$Chat_User = new Chat_User;
And use its function, like this, on my chat page also

<div id="userlst">
<?php

Code: Select all

$Chat_User->list_users();
?>
</div>

The above coding is doing great..
Here is my problem if I want to delete the user in the list by using the function on jquery like this event

$("#exit").click(function(){
var exit = confirm("Are you sure you want to end the chatting?");
if(exit==true){
window.location = 'index.php';
<?php $Chat_User->user_left($name); ?> //my php code to delete the user's listing
$.post("postlogOut.php");
}
});

Nothing happened what it done is when i refreshed the page the user will be deleted even if he/she is on the page.
As well as putting the code on the postlogOut.php to be manipulated their also nothing happened

Code: Select all

<?php
require("handlers/user_handler.php");
require("chat_module/Chat_User.class.php");

$name = $User->get_info("first_name");

$Chat_User = new Chat_User;

$Chat_User->user_left($name); //my php code to delete the user's listing

if(isset($_SESSION['name'])){
$fp = fopen("log.html", 'a');
fwrite($fp, "<div class='msgln'><i>User ".$_SESSION['name']." has left the chat session.</i><br/></div>");
fclose($fp);
}
?>
Please Help......I am really a newbie.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP Coding problem(Newbie)

Post by Christopher »

You can't put PHP in Javascript and have it run in the browser. You need to have the Javascript call your PHP script to do the delete. Something like: chat.php?op=delete&user=12345 . Then your chat.php can check if the $_GET['op'] is passed and call $chat->delete().
(#10850)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP Coding problem(Newbie)

Post by AbraCadaver »

I'm not sure how big of a deal it is with AJAX, but you should use POST method for ADD, UPDATE and DELETE operations.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply