PHP Ajax Message Board _php Tutorial

Source: Internet
Author: User
Tags mysql tutorial php mysql php mysql tutorial
PHP AJAX Message Board to provide a Chinese writing PHP Ajax message board, he is using the jquery PHP MySQL Ajax to implement the PHP Ajax local refresh reserved side board instances like to download it.

PHP Tutorial AJAX Message Board
To provide a Chinese writing PHP Ajax message board, he is using the jquery php MySQL tutorial ajax to implement the PHP Ajax local refresh reserved side board instances like to download it.
*/
$link = @mysql_connect ($db _host, $db _user, $db _pass) or Die (' unable to establish a DB connection ');

mysql_query ("Set names ' UTF8 '");
mysql_select_db ($db _database, $link);

Class Comment
{
Private $data = Array ();

Public function __construct ($row)
{
/*
/The constructor
*/

$this->data = $row;
}

Public Function Markup ()
{
/*
/This method outputs the XHTML markup of the comment
*/

Setting up a alias, so we don't have the to write $this->data every time:
$d = & $this->data;

$link _open = ";
$link _close = ";

if ($d [' url ']) {

If the person had entered a URL when adding a comment,
Define opening and closing hyperlink tags

$link _open = ";
$link _close = ";
}

Converting the time to a UNIX timestamp:
$d [' dt '] = strtotime ($d [' DT ']);

Needed for the default Gravatar image:
$url = ' http://'. DirName ($_server[' server_name '].$_server["Request_uri"]). ' /img/default_avatar.gif ';

Return '



'. $link _open. '

'. $link _close. '


'. $link _open. $d [' name ']. $link _close. '
'. Date (' d M y ', $d [' DT ']). '

'. $d [' body ']. '



';
}

public static function validate (& $arr)
{
/*
/This method was used to validate the data sent via Ajax.
/
/It return true/false depending on whether the data is valid, and populates
/The $arr array passed as a paremter (notice the ampersand above) with
/Either the valid input data, or the error messages.
*/

$errors = Array ();
$data = Array ();

Using the Filter_input function introduced in PHP 5.2.0

if (! ( $data [' email '] = filter_input (input_post, ' email ', filter_validate_email))
{
$errors [' email '] = ' Please enter a valid email. ';
}

if (! ( $data [' url '] = Filter_input (input_post, ' url ', Filter_validate_url))
{
If the URL field is not populated with a valid URL,
Act as if no URL is entered at all:

$url = ";
}

Using the filter with a custom callback function:

if (! ( $data [' body '] = filter_input (input_post, ' body ', filter_callback,array (' options ' = ' comment::validate_text ')))
{
$errors [' body '] = ' Please enter a comment body. ';
}

if (! ( $data [' name '] = Filter_input (input_post, ' name ', Filter_callback,array (' options ' = ' comment::validate_text ')))
{
$errors [' name '] = ' Please enter a name. ';
}

if (!empty ($errors)) {

If there is errors, copy the $errors array to $arr:

$arr = $errors;
return false;
}

If the data is valid, sanitize all the data and copy it to $arr:

foreach ($data as $k = = $v) {
$arr [$k] = mysql_real_escape_string ($v);
}

Ensure that e-mail is lower case:

$arr [' email '] = Strtolower (Trim ($arr [' email ')]);

return true;

}

private static function Validate_text ($STR)
{
/*
/This method is used internally as a Filter_callback
*/

if (Mb_strlen ($str, ' UTF8 ') <1)
return false;

Encode all HTML special characters (<,,;, ", &.. etc) and convert
The new line characters to
Tags

$str = NL2BR (Htmlspecialchars ($STR));

Remove the new line characters.
$str = Str_replace (the array (CHR), CHR), ", $STR);

return $str;
}

}

$comments = Array ();
$result = mysql_query ("SELECT * from comments ORDER by ID ASC");

while ($row = Mysql_fetch_assoc ($result))
{
$comments [] = new Comment ($row);
}

?>





Simple Ajax Commenting System | Tutorialzine Demo


/*
/Output The comments one by one:
*/

foreach ($comments as $c) {
echo $c->markup ();
}

?>


Add a Comment





Database Tutorial Structure

--
--Table structure for table ' comments '
--

Create TABLE ' comments ' (
' id ' int (ten) unsigned NOT NULL auto_increment,
' Name ' varchar (+) collate utf8_unicode_ci NOT null default ' ',
' URL ' varchar (255) COLLATE utf8_unicode_ci NOT null default ' ',
' Email ' varchar (255) COLLATE utf8_unicode_ci NOT null default ' ',
' Body ' text collate utf8_unicode_ci NOT NULL,
' DT ' timestamp NOT null default ' 0000-00-00 ',
Primary KEY (' ID ')
) Engine=myisam default Charset=utf8 collate=utf8_unicode_ci;

SOURCE download

Http://down.php100.com/down/code/php/liuyanboke/2010/1028/21479.html


http://www.bkjia.com/PHPjc/444830.html www.bkjia.com true http://www.bkjia.com/PHPjc/444830.html techarticle PHP AJAX Message board to provide a Chinese writing PHP Ajax message board, he is using the jquery PHP MySQL Ajax to implement the PHP Ajax local refresh reserved side board instances like to download it. PHP Tutorial Ajax ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.