Simple ajax comments complete code database structure CREATETABLE 'comments' ('id' int (10) unsignedNOTNULLauto_increment, 'name' varchar (128) collateutf8_unicode_ciNOTNULLdefault & amp; 39 simple ajax comments complete code database structure 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 does not null, 'dt' timestamp not null default CURRENT_TIMESTAMP, primary key ('id') ENGINE = MyISAM default charset = utf8 COLLATE = utf8_unicode_ci AUTO_INCREMENT = 1; demonstrate PHP Code PHP Code Markup () ;}?>
Add a Comment
Submit. phpPHP Code 1, 'html' => $ insertedComment-> markup ();} else {/* Outputtng the error messages */echo '{"status": 0, "errors ":'. json_encode ($ arr ). '}' ;}?> Comment. class. phpPHP Code 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 P HP 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
Tags: $ str = nl2br (htmlspecialchars ($ str); // Remove the new line characters that are left $ str = str_replace (array (chr (10 ), chr (13), '', $ str); return $ str ;}}?>