/ *
/ 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 Code
<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>
$ 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: * /
<div class = "name"> '. $ link_open. $ d [' name ']. $ link_close.' </ div>
<div class = "date" title = "Added at '.date (' H: i \ o \ nd 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 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);
}
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.