Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- CREATE TABLE IF NOT EXISTS `comments` (
- `commentid` int(11) unsigned NOT NULL AUTO_INCREMENT,
- `author` varchar(30) DEFAULT NULL,
- `text` text,
- `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (`commentid`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
- --
- -- Dumping data for table `comments`
- --
- INSERT INTO `comments` (`commentid`, `author`, `text`, `date`) VALUES
- (1, 'Alex', 'first comment', '2011-07-14 10:14:55'),
- (2, 'Alex', 'second comment comment', '2011-07-14 10:14:55'),
- (3, 'Another author', 'third comment', '2011-07-14 10:14:55');
- */
- header('Content-Type: text/html; charset=utf-8');
- // Database connection section
- mysql_connect('localhost', 'ajaxuser', 'phpguide');
- mysql_select_db('ajaxuser');
- mysql_query("SET NAMES 'UTF8'"); // what is utf???
- function output_comment($id, $author, $text, $date)
- {
- echo $id, ', ',
- htmlspecialchars($author), ', ',
- nl2br(htmlspecialchars($text)), ', ',
- $date, '<br/>';
- }
- // New comment posting
- if( isset($_POST['author'], $_POST['text']) )
- {
- $result = mysql_query("INSERT INTO `comments` (`author`, `text`, `date`)
- VALUES('". mysql_real_escape_string($_POST['author'])."', '". mysql_real_escape_string($_POST['text'])."', NOW()) ");
- if( $result == 1)
- {
- output_comment(mysql_insert_id(), $_POST['author'], $_POST['text'], date('Y-m-d H:i:s'));
- }
- else
- {
- echo mysql_error();
- }
- exit(0);
- }
- ?>
- <div id="list_of_comments" >
- <?php
- // Listing existing comments
- if( ($result = mysql_query("SELECT * FROM `comments` ORDER BY `commentid` ASC")) !== false)
- {
- while( $comment = mysql_fetch_assoc($result))
- {
- output_comment($comment['commentid'], $comment['author'], $comment['text'], $comment['date']);
- }
- }
- else
- {
- Echo 'The query has failed; ', mysql_error();
- }
- ?>
- </div>
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
- <form method="post" style="margin-top: 20px;" >
- author: <input type="text" name="author" /><br/>
- text: <textarea name="text" rows="2" cols="50" ></textarea><br/>
- <input type="submit" name="Send comment" />
- </form>
- <script type='text/javascript'>
- jQuery('form').submit(function() {
- jQuery.post('index.php', $(this).serialize(), comment_submitted);
- return false;
- });
- function comment_submitted(result)
- {
- jQuery('#list_of_comments').append(result);
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement