Advertisement
gitlez

YA: Simple Content Form WOC

Apr 11th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.44 KB | None | 0 0
  1. <?php
  2. /*    In response to a Yahoo Answer's Question    */
  3. // http://pastebin.com/2LHNNsqn  Same Script With Comments
  4.  
  5.  
  6. /*    Message Functions    */
  7. function errorMsg($msg){
  8.     if(is_array($msg)){
  9.         $r = '<table>';
  10.         foreach($msg as $m){
  11.             $r .= '<tr><td style="color: #900;font-weight: bold;">' . $m . '</td></tr>' . PHP_EOL;
  12.         }
  13.         return $r . '</table>';
  14.     }else{
  15.         return '<table><tr><td style="color: #900;font-weight: bold;">' . $msg . '</td></tr></table>';
  16.     }
  17. }
  18. function goodMsg($msg){
  19.     return '<table><tr><td style="color: #090;font-weight: bold;">' . $msg . '</td></tr></table>';
  20. }
  21.  
  22. /*    Variables    */
  23. $output = '';
  24. $title = $_POST['title'];
  25. $content = $_POST['content'];
  26. $category = $_POST['category'];
  27. $form = <<<FORM
  28. <form action="./test.php" method="post" style="background-color: #000;">
  29.     <table>
  30.         <tr>
  31.             <td><font color="white"><b>Title:</b></font></td>
  32.             <td><input type="text" name="title" value="$title"></td>
  33.         </tr>
  34.         <tr>
  35.             <td><font color="white"><b>Content:</b></font></td>
  36.             <td><textarea name="content" rows="6" col="80">$content</textarea></td>
  37.         </tr>
  38.         <tr>
  39.             <td><font color="white"><b>Category</b></font></td>
  40.             <td>
  41.             <select name="category">
  42.                 <option value="General Discussion">General Discussion</option>
  43.                 <option value="Suggestions">Suggestions</option>
  44.                 <option value="Complaints">Complaints</option>
  45.                 <option value="Problem Reporting">Problem Reporting</option>
  46.             </select>
  47.             </td>
  48.         </tr>
  49.         <tr>
  50.             <td colspan="2"><input type="submit" name="postbtn" value="Post"></td>
  51.         </tr>
  52.     </table>
  53. </form>
  54. FORM;
  55.  
  56. /*    Logged In Check    */
  57. $username = $_COOKIE['username'];
  58. $userid = $_COOKIE['userid'];
  59. if( !$username || !$userid){
  60.     header('Refresh: 5; url=/login/login.php');
  61.     echo errorMsg('You need to be logged in to access this page. <a href="/login/login.php" >Login</a>');
  62.     exit;
  63. }
  64.  
  65. /*    Form Checking And Processing    */
  66.  
  67. if ($_POST['postbtn']){
  68.     $currenttime = date("h:i A");
  69.     $currentdate = date("F d, Y");
  70.     $errors = Array();
  71.    
  72.     /*    Error Checking    */
  73.     if ((strlen($title) < 10) || (strlen($title) > 50)){
  74.         $errors[] = 'Title must be longer than 10 and less than 50 characters';
  75.     }
  76.     if ((strlen($content) < 50) || (strlen($content) > 1000)){
  77.         $errors[] = 'Content must be longer than 50 and less than 1000 characters';
  78.     }
  79.     if( count($errors) > 0){
  80.         $output = errorMsg($errors) . $form;
  81.     }else{
  82.         require ("connect.php");
  83.         $title = mysql_real_escape_string( $title );
  84.         $content = mysql_real_escape_string( $content );
  85.         $category = mysql_real_escape_string( $category );
  86.         $query = mysql_query("SELECT id FROM posts WHERE title='$title' LIMIT 1");
  87.         if ($query && mysql_num_rows($query) === 0){
  88.             if( mysql_query("INSERT INTO posts (title, content, time, date, author, category) VALUES ('$title', '$content', '$currenttime', 'on $currentdate', '$username', '$category')") ){
  89.                 header('Refresh: 5; url=./posts.php?title=' . $title);
  90.                 $output = goodMsg('Your message was posted successfully.<br>If you are not redirected to the post in 5 seconds. Click the link below.<br> <a href="">' . $title . '</a>');
  91.             }else{
  92.                 $output = errorMsg('There was an Internal Error, while attempting to save your post. Please try again in a few minutes.') . $form;
  93.             }
  94.         }else{
  95.             $output = errorMsg('There is already a post with that Title, please select another.') . $form;
  96.         }
  97.         // mysql_close($conn);
  98.     }
  99. }else{
  100.     $output = $form;
  101. }
  102. echo $output;
  103. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement