|
$ 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 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 = '<a href = "'. $ d ['URL']. '"> ';
$ Link_close = '</a> ';
}
// 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'
<Div class = "comment">
<Div class = "avatar">
'. $ Link_open .'
'. $ Link_close .'
</Div>
<Div class = "name"> '. $ link_open. $ d ['name']. $ link_close.' </div>
<Div class = "date" title = "added '. date ('H: I on d m Y', $ d ['dt']). '"> '. date ('d m Y', $ d ['dt']). '</div>
<P> '. $ d ['body'].' </p>
</Div>
';
}
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 abve)
/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 <br> tags:
$ Str = nl2br (htmlspecialchars ($ str ));
// Remove the new line characters that are left
$ Str = str_replace (array (chr (10), chr (13), '', $ 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 );
}
?>
<! 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">
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = gb2312"/>
<Title> simple ajax commenting system | tutorialine demo </title>
<Link rel = "stylesheet" type = "text/css tutorial" href = "styles.css"/>
</Head>
<Body>
<Div id = "main">
<? Php
/*
/Output the comments one by one:
*/
Foreach ($ comments as $ c ){
Echo $ c-> markup ();
}
?>
<Div id = "addcommentcontainer">
<P> add a comment </p>
<Form id = "addcommentform" method = "post" action = "">
<Div>
<Label for = "name"> your name </label>
<Input type = "text" name = "name" id = "name"/>
<Label for = "email"> your email </label>
<Input type = "text" name = "email" id = "email"/>
<Label for = "url"> website (not required) </label>
<Input type = "text" name = "url" id = "url"/>
<Label for = "body"> comment body </label>
<Textarea name = "body" id = "body" cols = "20" rows = "5"> </textarea>
<Input type = "submit" id = "submit" value = "submit"/>
</Div>
</Form>
</Div>
</Div>
<Script type = "text/webpage effects" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> </script>
<Script type = "text/javascript" src = "script. js"> </script>
</Body>
</Html> |