简单评论php,简单实用方便的ajax评论完整代码_php

简单的ajax评论完整代码

数据库结构CREATE TABLE `comments` (

`id` int(10) unsigned NOT NULL auto_increment,

`name` varchar(128) 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 CURRENT_TIMESTAMP,

PRIMARY KEY  (`id`)

) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;

演示

[php]

// Error reporting:

error_reporting(E_ALL^E_NOTICE);

include "conn.php";

include "comment.class.php";

/* (PS:^_^不错的php学习交流群:276167802,验证:csl,有兴趣的话可以加入进来一起讨论)

/   Select all the comments and populate the $comments array with objects

*/

$comments = array();

$result = mysql_query("SELECT * FROM comments ORDER BY id ASC");

while($row = mysql_fetch_assoc($result))

{

$comments[] = new Comment($row);

}

?>

PHP

[php]

/*

/   Output the comments one by one:

*/

foreach($comments as $c){

echo $c->markup();

}

?>

Add a Comment

Your Name

Your Email

website (not required)

Comment Body

submit.php

PHP

[php]

// Error reporting:

error_reporting(E_ALL^E_NOTICE);

include "conn.php";

include "comment.class.php";

/*

/   This array is going to be populated with either

/   the data that was sent to the script, or the

/   error messages.

/*/

$arr = array();

$validates = Comment::validate($arr);

if($validates)

{

/* Everything is OK, insert to database: */

mysql_query("   INSERT INTO comments(name,url,email,body)

VALUES (

'".$arr['name']."',

'".$arr['url']."',

'".$arr['email']."',

'".$arr['body']."'

)");

$arr['dt'] = date('r',time());

$arr['id'] = mysql_insert_id();

/*

/   The data in $arr is escaped for the mysql query,

/   but we need the unescaped variables, so we apply,

/   stripslashes to all the elements in the array:

/*/

$arr = array_map('stripslashes',$arr);

$insertedComment = new Comment($arr);

/* Outputting the markup of the just-inserted comment: */

echo json_encode(array('status'=>1,'html'=>$insertedComment->markup()));

}

else

{

/* Outputtng the error messages */

echo '{"status":0,"errors":'.json_encode($arr).'}';

}

?>

comment.class.php

PHP

[php]

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 an alias, so we don't have to write $this->data every time:

$d = &$this->data;

$link_open = '';

$link_close = '';

if($d['url']){

// If the person has 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 is 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 was not populated with a valid URL,

// act as if no URL was 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 are 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 the email 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 that are left

$str = str_replace(array(chr(10),chr(13)),'',$str);

return $str;

}

}

?>

以上是本文关于ajax评论的完整代码,希望本文对广大php开发者有所帮助,感谢阅读本文。

欢迎大家阅读《简单实用方便的ajax评论完整代码_php》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码

e7ce419cf2d6ad34d01da2ceb8829eed.png

微信 赏一包辣条吧~

023a57327877fb4402bcc76911ec18ea.png

支付宝 赏一听可乐吧~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值