Ciao a tutti. Ho scaricato una shoutbox/tagboard da inserire in un forum phpBB3. Premetto che non ho ben presente come funzioni Jquery, per questo vi chiedo aiuto.
Ci sono due problemi:
- il primo è che, nonostante i messaggi vengano inseriti correttamente nella base dati, mi stampa "Database connection problem!";
- il secondo è il fatto che, dopo aver inserito una quindicina di messaggi, dà errore e non fa né inserire nuovi messaggi né vedere quelli vecchi.
Questo è il codice che genera la tabella:
Ecco i file principali (scusate ma mi tocca fare due post consecutivi perchè è troppo lungo)Codice:CREATE TABLE IF NOT EXISTS `shouts` ( `id` int(11) NOT NULL auto_increment, `nickname` varchar(50) NOT NULL, `message` varchar(255) NOT NULL, `date_added` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
"index.php"
"php/getShouts.php"Codice HTML:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US" dir="ltr"> <head> <title>InsanityVille » Shoutbox</title> <link rel="stylesheet" type="text/css" href="style/style.css"> <meta name="Title" content="InsanityVille"> <meta name="Author" content="Mocanu George-Adrian"> <meta name="Subject" content="InsanityVille Special Stuff"> <meta name="Description" content="InsanityVille Scripts Demos. AJAX Shoutbox"> <meta name="Keywords" content="insain scripts, php scripts, ajax shoutbox, jquery, ajax, shoutbox, jquery validation, ajax link checker, link cheker"> <meta name="Language" content="english"> <meta name="Distribution" content="free"> <meta name="Robots" content="All"> <meta name="verify-v1" content="r/NImAjDz0HRiUAolYgw6ctQfuyYPuRHvhpSgyiYDnA=" /> <script src="js/jquery.js" type="text/javascript" language="javascript"></script> <script src="js/code.js" type="text/javascript" language="javascript"></script> </head> <body> <div id="content"> <div id="header"> <a href="http://shoutbox.insanityville.com/"><h1>Shoutbox</h1></a> <p>Check out the demos for the scripts <a href="http://insanityville.com" target="_blank" title="InsanityVille">InsanityVille</a> is offering for free download.</p> </div> <table> <tr> <td width="68%"> <h2>InsanityVille's AJAX Shoutbox <span style="font: normal xx-small Verdana;">v 0.1</span></h2> <br />This Shoutbox is another exemplification of the power of AJAX.<br /> <h2>Please fill out the form:</h2> <div class="formContainer"> <div class="form"> Nickname: <input type="text" id="shoutNickname" maxlength="50" size="10" class="textInput" value="<? echo $user->data['username']; ?>" /> Message: <input type="text" size="46" id="shoutMessage" maxlength="100" class="textInput" /> <input type="button" id="doShout" class="textInput" value="SHOUT!" /> <span id="msgbox" style="display:none"></span> </div> <div class="clear"> </div> <div class="status"><span id="shoutStatus" class="shoutStatus">Fill in the fields above to shout!</span></div> <div class="clear"> </div> <div class="shouts" id="shoutsContainer"></div> </div> </td> </tr> </table> <p id="footer">© 2008 InsanityVille. All rights reserved.</p> </div> </body> </html>
"php/classes/shoutBox.class"Codice PHP:<?php
[...]
include('classes/shoutBox.class.php');
header('Content-type: text/xml');
$sb = new Shoutbox($_POST['nickname'], $_POST['message'], $_POST['op']);
echo $sb->response;
?>
Codice PHP:<?php
class Shoutbox {
public $response;
public function Shoutbox($nick = null, $msg = null, $op = null) {
if(!$nick && !$msg && $op == 'tick') {
$this->returnShouts();
}
if($op == 'newShout') {
$this->validateData($nick, $msg);
}
}
public function returnShouts($status = null, $error = null) {
$query = execute_query('SELECT * FROM `shouts` ORDER BY `id` DESC LIMIT 0, 10');
$response = '';
if(!$query) {
$response .= '<response>';
$response .= '<error>Database connection problem!</error>';
$response .= '</response>';
} else {
$noShouts = count($query);
if($noShouts == 0) {
$response .= '<response>';
$response .= '<error>No shouts yet!</error>';
$response .= '</response>';
} else {
$response .= '<response>';
foreach ($query as $shout) {
$response .= '<shout>';
$response .= '<nickname>'.$shout['nickname'].'</nickname>';
$response .= '<message>'.$shout['message'].'</message>';
$response .= '<date>'.$this->showTime($shout['date_added']).'</date>';
$response .= '</shout>';
}
$response .= '<status>'.$status.'</status>';
$response .= '<error>'.$error.'</error>';
$response .= '</response>';
}
}
$this->response = $response;
}
private function validateData($nick = null, $msg = null) {
$nick = $this->strFilter($nick);
$msg = $this->strFilter($msg);
if(empty($nick)) {
$this->returnShouts(null, 'Don\'t be shy, enter your nickname.');
return false;
}
elseif(empty($msg)) {
$this->returnShouts(null, 'It doesn\'t seem that you want to shout at all! Write the message.');
return false;
}
else {
$this->addShout($nick, $msg);
}
}
private function strFilter($string) {
/* $string = str_replace("http://", "", $string);
$string = str_replace("<", "", $string);
$string = str_replace(">", "", $string);
*/
$string = htmlentities($string);
$string = addslashes($string);
return $string;
}
private function showTime($data_comment = null) {
$now = date('Y-m-d H:i:s');
$data_comment = urldecode($data_comment);
$data_comment_data = substr($data_comment, 0, 10);
$data_comment_curenta = substr($now, 0, 10);
$nrsecunde = (strtotime($now) - strtotime($data_comment));
$nrminute = $nrsecunde / (60);
$nrore = $nrminute / (60);
$nroreafisat = intval($nrore);
$nrminuteafisat = intval($nrminute - ($nroreafisat * 60));
$nrsecundeafisat = intval($nrsecunde - ($nroreafisat * 60 * 60) - ($nrminuteafisat * 60));
if ($nroreafisat == 1 ) {$ore = 'ora';} else {$ore = 'ore';}
if ($nrminuteafisat == 1 ) {$minute = 'minuto';} else {$minute = 'minuti';}
if ($nrsecundeafisat == 1 ) {$secunde = 'secondo';} else {$secunde = 'secondi';}
if($nroreafisat > 0 && $nroreafisat <= 23 && $nrminuteafisat > 0) {
return $nroreafisat.' '.$ore.' '.'e'.' '.$nrminuteafisat.' '.$minute.' '.'fa';
}
elseif($nroreafisat == 0 && $nrminuteafisat > 0) {
return $nrminuteafisat.' '.$minute.' '.'e'.' '.$nrsecundeafisat.' '.$secunde.' '.'fa';
}
elseif($nroreafisat == 0 && $nrminuteafisat == 0) {
return $nrsecundeafisat.' '.$secunde.' '.'fa';
}
elseif ($nroreafisat > 24) {
// return $data_comment;
return date('d-m-Y H:i:s', strtotime($data_comment));
}
}
private function addShout($nickname = null, $message = null) {
$time = date('Y-m-d H:i:s');
$query = execute_query("INSERT INTO `shouts` (`nickname`, `message`, `date_added`) VALUES ('".$nickname."', '".$message."','".$time."');");
/* if(!$query) {
$this->returnShouts(null, 'Database connection problem!');
} else {
$this->returnShouts('Shout added successfully');
}
*/ $this->returnShouts();
}
}
?>

LinkBack URL
About LinkBacks
