title: Creating PHP Forms with show/hide functionality
body: <p>I want to create two reports and submit the report data to database by using two functions defined in a class: Here I have two buttons: "Create ES" and "Create RP".</p>
  
  <p>Rightnow, my forms are working fine, I can insert data successfully, but the problem was when I click on submit after filling the form data, the content is hiding and displays the fist div content "cs_content" and again I need to onclick to submit again.</p>
  
  <p>Could anyone give a solution for this.</p>
  
  <p>Requirement :</p>
  
  <p>When I click on "Create CS",  I should be able to fill the form and submit data successfully with a message within "cs_content" and any form input errors, the errors should display within "cs_content".</p>
  
  <p>When I click on "Create RP",  I should be able to fill the form and submit data successfully with a message within "rp_content" and any form input errors, the errors should display within "rp_content".</p>
  
  <p>home.php</p>
  
  <pre><code>&lt;?php
   require 'classes/class.report.php';
   $report = new Report($db); 
  ?&gt;
  &lt;html&gt;
    &lt;head&gt;      
     &lt;script src="js/jqueryv1.10.2.js"&gt;&lt;/script&gt;
     &lt;script&gt;
  
         $ (document).ready(function () 
         {
            //$("#cs_content").show();
            $('#cs').click(function () 
            {
              $('#cs_content').fadeIn('slow');
              $('#rp_content').hide();
            });
            $('#rp').click(function () 
            {
              $('#rp_content').fadeIn('slow');
              $('#cs_content').hide();
            });
         });
     &lt;/script&gt;
    &lt;/head&gt;  
    &lt;body&gt;
       &lt;div class="container2"&gt;
             &lt;div style="margin:0px 0px;padding:3px 217px;overflow:hidden;"&gt;
               &lt;div id="cs" style="float:left;margin:0px 0px;padding:7px;"&gt;&lt;input type="button" value="CREATE CS"&gt;&lt;/div&gt;
               &lt;div id="rp" style="float:left;margin:0px 0px;padding:7px;"&gt;&lt;input type="button" value="CREATE RP"&gt;&lt;/div&gt;&lt;br&gt;
             &lt;/div&gt;
             &lt;div id="cs_content"&gt; &lt;?php $report-&gt;create_cs_report(); ?&gt; &lt;/div&gt;            
             &lt;div id="rp_content" style="display:none;"&gt; &lt;?php $report-&gt;create_rp_report(); ?&gt; &lt;/div&gt;        
       &lt;/div&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>class.report.php</p>
  
  <pre><code>&lt;?php
      class Report
      {
         private $db;
  
         public function __construct($database){
            $this-&gt;db = $database;
         }
  
         public function create_cs_report()
         {
            if (isset($_POST['create_es_report']))
            {      
               $report_name = htmlentities($_POST['report_name']);            
               $from_address = htmlentities($_POST['from_address']); 
               $subject = htmlentities($_POST['subject']); 
               $reply_to = htmlentities($_POST['reply_to']); 
  
  
               if (empty($_POST['report_name']) || empty($_POST['from_address']) || empty($_POST['subject']) || empty($_POST['reply_to']))
               {
                  $errors[] = '&lt;span class="error"&gt;All fields are required.&lt;/span&gt;';
               }
               else
               {
                  if (isset($_POST['report_name']) &amp;&amp; empty($_POST['report_name'])) { $errors[] = '&lt;span class="error"&gt;Report Name is required&lt;/span&gt;'; }
                  else if (!ctype_alnum($_POST['report_name']))
                  {  $errors[] = '&lt;span class="error"&gt;Report Name: Whitespace is not allowed, only alphabets and numbers are required&lt;/span&gt;';  }
  
                  if (isset($_POST['from_address']) &amp;&amp; empty($_POST['from_address'])) 
                  { $errors[] = '&lt;span class="error"&gt;From address is required&lt;/span&gt;'; }
                  else if (filter_var($_POST['from_address'], FILTER_VALIDATE_EMAIL) === false)
                  { $errors[] = '&lt;span class="error"&gt;Please enter a valid From address&lt;/span&gt;';  }
  
                  if (isset($_POST['subject']) &amp;&amp; empty($_POST['subject'])) { $errors[] = '&lt;span class="error"&gt;Subject is required&lt;/span&gt;'; }
                  else if (!ctype_alnum($_POST['subject']))
                  {  $errors[] = '&lt;span class="error"&gt;Subject: Whitespace is not allowed, only alphabets and numbers are required&lt;/span&gt;';  }
  
                  if (isset($_POST['reply_to']) &amp;&amp; empty($_POST['reply_to'])) { $errors[] = '&lt;span class="error"&gt;Reply To is required&lt;/span&gt;'; }
                  else if (filter_var($_POST['reply_to'], FILTER_VALIDATE_EMAIL) === false)
                  { $errors[] = '&lt;span class="error"&gt;Please enter a valid Reply-To address&lt;/span&gt;';  }                        
               }
  
               if (empty($errors) === true)
               {               
                   $query = $this-&gt;db-&gt;prepare("INSERT INTO report(report_name, from_address, subject, reply_to) VALUES (?, ?, ?, ?) ");
  
                   $query-&gt;bindValue(1, $report_name);                
                   $query-&gt;bindValue(2, $from_address);
                   $query-&gt;bindValue(3, $subject);         
                   $query-&gt;bindValue(4, $reply_to);                 
  
                   try {
                     $query-&gt;execute();            
                   }
  
                   catch(PDOException $e) {
                      die($e-&gt;getMessage());
                   }  
                   header('Location:home.php?success');
                   exit();                 
               }
            } 
  
            if (isset($_GET['success']) &amp;&amp; empty($_GET['success'])) 
            { 
                header('Location:home.php');
                echo '&lt;span class="error"&gt;Report is succesfully created&lt;/span&gt;';  
            }
  
            ?&gt;
  
            &lt;form action="" method="POST" accept-charset="UTF-8"&gt;
                &lt;div style="font-weight:bold;padding:17px 80px;text-decoration:underline;"&gt;Section A&lt;/div&gt;
                &lt;table class="create_report"&gt;                           
                  &lt;tr&gt;&lt;td&gt;&lt;label&gt;Report Name&lt;/label&gt;&lt;span style="color:#A60000"&gt;*&lt;/span&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" name="report_name" required placeholder="Name of the report" value="&lt;?php if(isset($_POST["report_name"])) echo $report_name; ?&gt;" size="30" maxlength="30"&gt;             
                  &lt;/td&gt;&lt;/tr&gt;              
  
                    &lt;tr&gt;&lt;td&gt;&lt;label&gt;From&lt;/label&gt;&lt;span style="color:#A60000"&gt;*&lt;/span&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;input type="text" name="from_address" required placeholder="From address" value="&lt;?php if(isset($_POST["from_address"])) echo $from_address; ?&gt;" size="30"&gt;             
                    &lt;/td&gt;&lt;/tr&gt;
  
                    &lt;tr&gt;&lt;td&gt;&lt;label&gt;Subject&lt;/label&gt;&lt;span style="color:#A60000"&gt;*&lt;/span&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;input type="text" name="subject" required placeholder="Subject" value="&lt;?php if(isset($_POST["subject"])) echo $subject; ?&gt;" size="30"&gt;             
                    &lt;/td&gt;&lt;/tr&gt;
  
                    &lt;tr&gt;&lt;td&gt;&lt;label&gt;Reply To&lt;/label&gt;&lt;span style="color:#A60000"&gt;*&lt;/span&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;input type="text" name="reply_to" required placeholder="Reply address" value="&lt;?php if(isset($_POST["reply_to"])) echo $reply_to; ?&gt;" size="30"&gt;            
                    &lt;/td&gt;&lt;/tr&gt;
  
                 &lt;tr&gt;&lt;td&gt;&lt;input type="submit" value="create report" style="background:#8AC007;color:#080808;padding:6px;" name="create_es_report"&gt;&lt;/td&gt;&lt;/tr&gt; 
               &lt;/table&gt;                       
            &lt;/form&gt;
  
            &lt;?php
              //IF THERE ARE ERRORS, THEY WOULD BE DISPLAY HERE
              if (empty($errors) === false) {
                 echo '&lt;div&gt;' . implode('&lt;/p&gt;&lt;p&gt;', $errors) . '&lt;/div&gt;';
              }                  
        }  
  
  
        public function create_rp_report()
         {
            if (isset($_POST['create_rp_report']))
            {      
               $report_name = htmlentities($_POST['report_name']);            
               $to_address = htmlentities($_POST['to_address']); 
               $subject = htmlentities($_POST['subject']); 
               $reply_to = htmlentities($_POST['reply_to']); 
  
  
               if (empty($_POST['report_name']) || empty($_POST['to_address']) || empty($_POST['subject']) || empty($_POST['reply_to']))
               {
                  $errors[] = '&lt;span class="error"&gt;All fields are required.&lt;/span&gt;';
               }
               else
               {
                  if (isset($_POST['report_name']) &amp;&amp; empty($_POST['report_name'])) { $errors[] = '&lt;span class="error"&gt;Report Name is required&lt;/span&gt;'; }
                  else if (!ctype_alnum($_POST['report_name']))
                  {  $errors[] = '&lt;span class="error"&gt;Report Name: Whitespace is not allowed, only alphabets and numbers are required&lt;/span&gt;';  }
  
                  if (isset($_POST['to_address']) &amp;&amp; empty($_POST['to_address'])) 
                  { $errors[] = '&lt;span class="error"&gt;to address is required&lt;/span&gt;'; }
                  else if (filter_var($_POST['to_address'], FILTER_VALIDATE_EMAIL) === false)
                  { $errors[] = '&lt;span class="error"&gt;Please enter a valid to address&lt;/span&gt;';  }
  
                  if (isset($_POST['subject']) &amp;&amp; empty($_POST['subject'])) { $errors[] = '&lt;span class="error"&gt;Subject is required&lt;/span&gt;'; }
                  else if (!ctype_alnum($_POST['subject']))
                  {  $errors[] = '&lt;span class="error"&gt;Subject: Whitespace is not allowed, only alphabets and numbers are required&lt;/span&gt;';  }
  
                  if (isset($_POST['reply_to']) &amp;&amp; empty($_POST['reply_to'])) { $errors[] = '&lt;span class="error"&gt;Reply To is required&lt;/span&gt;'; }
                  else if (filter_var($_POST['reply_to'], FILTER_VALIDATE_EMAIL) === false)
                  { $errors[] = '&lt;span class="error"&gt;Please enter a valid Reply-To address&lt;/span&gt;';  }                        
               }
  
               if (empty($errors) === true)
               {               
                   $query = $this-&gt;db-&gt;prepare("INSERT INTO report(report_name, to_address, subject, reply_to) VALUES (?, ?, ?, ?) ");
  
                   $query-&gt;bindValue(1, $report_name);                
                   $query-&gt;bindValue(2, $to_address);
                   $query-&gt;bindValue(3, $subject);         
                   $query-&gt;bindValue(4, $reply_to);                 
  
                   try {
                     $query-&gt;execute();            
                   }
  
                   catch(PDOException $e) {
                      die($e-&gt;getMessage());
                   }  
                   header('Location:home.php?success');
                   exit();                 
               }
            } 
  
            if (isset($_GET['success']) &amp;&amp; empty($_GET['success'])) 
            { 
                header('Location:home.php');
                echo '&lt;span class="error"&gt;Report is succesfully created&lt;/span&gt;';  
            }
  
            ?&gt;
  
            &lt;form action="" method="POST" accept-charset="UTF-8"&gt;
                &lt;div style="font-weight:bold;padding:17px 80px;text-decoration:underline;"&gt;Section A&lt;/div&gt;
                &lt;table class="create_report"&gt;                           
                  &lt;tr&gt;&lt;td&gt;&lt;label&gt;Report Name&lt;/label&gt;&lt;span style="color:#A60000"&gt;*&lt;/span&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" name="report_name" required placeholder="Name of the report" value="&lt;?php if(isset($_POST["report_name"])) echo $report_name; ?&gt;" size="30" maxlength="30"&gt;             
                  &lt;/td&gt;&lt;/tr&gt;              
  
                    &lt;tr&gt;&lt;td&gt;&lt;label&gt;to&lt;/label&gt;&lt;span style="color:#A60000"&gt;*&lt;/span&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;input type="text" name="to_address" required placeholder="to address" value="&lt;?php if(isset($_POST["to_address"])) echo $to_address; ?&gt;" size="30"&gt;             
                    &lt;/td&gt;&lt;/tr&gt;
  
                    &lt;tr&gt;&lt;td&gt;&lt;label&gt;Subject&lt;/label&gt;&lt;span style="color:#A60000"&gt;*&lt;/span&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;input type="text" name="subject" required placeholder="Subject" value="&lt;?php if(isset($_POST["subject"])) echo $subject; ?&gt;" size="30"&gt;             
                    &lt;/td&gt;&lt;/tr&gt;
  
                    &lt;tr&gt;&lt;td&gt;&lt;label&gt;Reply To&lt;/label&gt;&lt;span style="color:#A60000"&gt;*&lt;/span&gt;&lt;/td&gt;
                        &lt;td&gt;&lt;input type="text" name="reply_to" required placeholder="Reply address" value="&lt;?php if(isset($_POST["reply_to"])) echo $reply_to; ?&gt;" size="30"&gt;            
                    &lt;/td&gt;&lt;/tr&gt;
  
                 &lt;tr&gt;&lt;td&gt;&lt;input type="submit" value="create report" style="background:#8AC007;color:#080808;padding:6px;" name="create_rp_report"&gt;&lt;/td&gt;&lt;/tr&gt; 
               &lt;/table&gt;                       
            &lt;/form&gt;
  
            &lt;?php
              //IF THERE ARE ERRORS, THEY WOULD BE DISPLAY HERE
              if (empty($errors) === false) {
                 echo '&lt;div&gt;' . implode('&lt;/p&gt;&lt;p&gt;', $errors) . '&lt;/div&gt;';
              }                  
        }   
  
     }//Report CLASS ENDS   
  </code></pre>