November 18, 201214 yr Hi guys, I followed a tutorial from phpacademy but there is one thing that isn't working Well the javascript is working and the chat application display all the messages in the database. But when I send a message to the database, it just doesn't get in there... Thanks PS: This is the code where I think it could go wrong: ajax/chat.php <?php require '../core/init.php'; if (isset($_POST['method']) === true && empty($_POST['method']) === false) { $chat = new Chat(); $method = trim($_POST['method']); if ($method === 'fetch') { $messages = $chat->fetchMessages(); if (empty($messages) === true) { echo 'There are currently no messages in the chat'; } else { foreach($messages as $message) { ?> <div class="message"> <a href="#"><?php echo $message['username']; ?></a> says: <p><?php echo nl2br($message['message']); ?></p> </div> <?php } } } else if ($method === 'throw' && isset($_POST['message']) === true) { $message = trim($_POST['message']); if (empty($message) === false) { $chat->throwMessage($_SESSION['user'], $message); } } } classes/Core.php <?php class Core { protected $db, $result; private $rows; public function __construct() { $this->db = new mysqli('localhost', 'root', '', 'chat'); } public function query($sql) { $this->result = $this->db->query($sql); } public function rows() { for($x = 1; $x <= $this->db->affected_rows; $x++) { $this->rows[] = $this->result->fetch_assoc(); } return $this->rows; } } classes/Chat.php <?php class Chat extends Core { public function fetchMessages() { $this->query(" SELECT `chat`.`message`, `users`.`username`, `users`.`user_id` FROM `chat` JOIN `users` ON `chat`.`user_id` = `users`.`user_id` ORDER BY `chat`.`timestamp` DESC "); return $this->rows(); } public function throwMessage($user_id, $message) { $this->query(" INSERT INTO `chat` (`user_id`, `message`, `timestamp`) VALUES (" . (int)$user_id . ", '" . $this->db->real_escape_string(htmlentities($message)) . "', UNIX_TIMESTAMP()) "); } }
November 18, 201214 yr Author Try using Firebug in Firefox - this way you can see the AJAX request happen in real time, aswell as seeing the response from your AJAX script too. Well my Ajax is working fine, but the PHP won't send any data to the database But I try firebug, to be sure Thanks
November 18, 201214 yr Well my Ajax is working fine, but the PHP won't send any data to the database But I try firebug, to be sure Thanks then ur ajax may not be passing the data properly so it would be good to check that anyways
November 18, 201214 yr Author Well I checked the code, but their are no errors at all... I made a check with a echo to be sure that the data is send, thats works fine when I post something It only doesn't appear in the database, so it must be something in there...
November 25, 201214 yr URL Encode your strings to be sent through AJAX. I know that # signs are a common issue when being ajax'd, and that everything should be turned into an html entity. If that's not the problem, perhaps there is a problem when inserting the information into the DB.
Create an account or sign in to comment