Advertisement
intval

Untitled

Jul 14th, 2011
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.77 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.   CREATE TABLE IF NOT EXISTS `comments` (
  5.   `commentid` int(11) unsigned NOT NULL AUTO_INCREMENT,
  6.   `author` varchar(30) DEFAULT NULL,
  7.   `text` text,
  8.   `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  9.   PRIMARY KEY (`commentid`)
  10. ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
  11.  
  12. --
  13. -- Dumping data for table `comments`
  14. --
  15.  
  16. INSERT INTO `comments` (`commentid`, `author`, `text`, `date`) VALUES
  17. (1, 'Alex', 'first comment', '2011-07-14 10:14:55'),
  18. (2, 'Alex', 'second comment comment', '2011-07-14 10:14:55'),
  19. (3, 'Another author', 'third comment', '2011-07-14 10:14:55');
  20.  */
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28. header('Content-Type: text/html; charset=utf-8');
  29.  
  30. // Database connection section
  31.     mysql_connect('localhost', 'ajaxuser', 'phpguide');
  32.     mysql_select_db('ajaxuser');
  33.     mysql_query("SET NAMES 'UTF8'"); // what is utf???
  34.    
  35.  
  36.    
  37.     function output_comment($id, $author, $text, $date)
  38.     {
  39.         echo $id, ', ',
  40.              htmlspecialchars($author), ', ',
  41.              nl2br(htmlspecialchars($text)), ', ',
  42.              $date, '<br/>';
  43.     }
  44.    
  45.    
  46. // New comment posting
  47.    
  48.     if( isset($_POST['author'], $_POST['text']) )
  49.     {
  50.         $result = mysql_query("INSERT INTO `comments` (`author`, `text`, `date`)
  51.                     VALUES('".  mysql_real_escape_string($_POST['author'])."', '".  mysql_real_escape_string($_POST['text'])."', NOW()) ");
  52.        
  53.         if( $result == 1)
  54.         {
  55.             output_comment(mysql_insert_id(), $_POST['author'], $_POST['text'], date('Y-m-d H:i:s'));
  56.         }
  57.         else
  58.         {
  59.             echo mysql_error();
  60.         }
  61.        
  62.         exit(0);
  63.     }
  64.    
  65. ?>
  66.  
  67.  
  68.  
  69. <div id="list_of_comments" >
  70. <?php
  71. // Listing existing comments
  72.  
  73.     if( ($result = mysql_query("SELECT * FROM `comments` ORDER BY `commentid` ASC"))  !== false)
  74.     {
  75.         while( $comment = mysql_fetch_assoc($result))
  76.         {
  77.              output_comment($comment['commentid'], $comment['author'], $comment['text'], $comment['date']);
  78.         }
  79.     }
  80.     else
  81.     {
  82.         Echo 'The query has failed; ', mysql_error();
  83.     }
  84.  
  85. ?>
  86. </div>
  87.  
  88.  
  89.  
  90. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
  91. <form method="post" style="margin-top: 20px;" >
  92.     author: <input type="text" name="author" /><br/>
  93.     text: <textarea name="text" rows="2" cols="50" ></textarea><br/>
  94.    
  95.     <input type="submit" name="Send comment" />
  96. </form>
  97.  
  98. <script type='text/javascript'>
  99.     jQuery('form').submit(function() {
  100.       jQuery.post('index.php', $(this).serialize(), comment_submitted);
  101.       return false;
  102.     });
  103.    
  104.     function comment_submitted(result)
  105.     {
  106.         jQuery('#list_of_comments').append(result);
  107.     }
  108.    
  109. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement