Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Chat application with Ajax, won't send message to db

Featured Replies

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 icon_e_biggrin.gif

 

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())
");
}
}
  • 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

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

  • 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...

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.