使用显示/隐藏功能创建PHP表单

时间:2014-06-11 02:40:19

标签: php jquery

我想创建两个报告,并使用类中定义的两个函数将报告数据提交到数据库:这里有两个按钮:“Create ES”和“Create RP”。

Rightnow,我的表单工作正常,我可以成功插入数据,但问题是当我在填写表单数据后点击提交,内容隐藏并显示第一个div内容“cs_content”,我需要再次onclick再次提交。

任何人都可以为此提供解决方案。

要求:

当我点击“创建CS”时,我应该能够填写表格并使用“cs_content”中的消息成功提交数据,并且任何表单输入错误,错误都应显示在“cs_content”中。

当我点击“创建RP”时,我应该能够填写表格并成功提交数据,并在“rp_content”内显示任何表格输入错误,错误应显示在“rp_content”中。

home.php

<?php
 require 'classes/class.report.php';
 $report = new Report($db); 
?>
<html>
  <head>      
   <script src="js/jqueryv1.10.2.js"></script>
   <script>

       $ (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();
          });
       });
   </script>
  </head>  
  <body>
     <div class="container2">
           <div style="margin:0px 0px;padding:3px 217px;overflow:hidden;">
             <div id="cs" style="float:left;margin:0px 0px;padding:7px;"><input type="button" value="CREATE CS"></div>
             <div id="rp" style="float:left;margin:0px 0px;padding:7px;"><input type="button" value="CREATE RP"></div><br>
           </div>
           <div id="cs_content"> <?php $report->create_cs_report(); ?> </div>            
           <div id="rp_content" style="display:none;"> <?php $report->create_rp_report(); ?> </div>        
     </div>
  </body>
</html>

class.report.php

<?php
    class Report
    {
       private $db;

       public function __construct($database){
          $this->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[] = '<span class="error">All fields are required.</span>';
             }
             else
             {
                if (isset($_POST['report_name']) && empty($_POST['report_name'])) { $errors[] = '<span class="error">Report Name is required</span>'; }
                else if (!ctype_alnum($_POST['report_name']))
                {  $errors[] = '<span class="error">Report Name: Whitespace is not allowed, only alphabets and numbers are required</span>';  }

                if (isset($_POST['from_address']) && empty($_POST['from_address'])) 
                { $errors[] = '<span class="error">From address is required</span>'; }
                else if (filter_var($_POST['from_address'], FILTER_VALIDATE_EMAIL) === false)
                { $errors[] = '<span class="error">Please enter a valid From address</span>';  }

                if (isset($_POST['subject']) && empty($_POST['subject'])) { $errors[] = '<span class="error">Subject is required</span>'; }
                else if (!ctype_alnum($_POST['subject']))
                {  $errors[] = '<span class="error">Subject: Whitespace is not allowed, only alphabets and numbers are required</span>';  }

                if (isset($_POST['reply_to']) && empty($_POST['reply_to'])) { $errors[] = '<span class="error">Reply To is required</span>'; }
                else if (filter_var($_POST['reply_to'], FILTER_VALIDATE_EMAIL) === false)
                { $errors[] = '<span class="error">Please enter a valid Reply-To address</span>';  }                        
             }

             if (empty($errors) === true)
             {               
                 $query = $this->db->prepare("INSERT INTO report(report_name, from_address, subject, reply_to) VALUES (?, ?, ?, ?) ");

                 $query->bindValue(1, $report_name);                
                 $query->bindValue(2, $from_address);
                 $query->bindValue(3, $subject);         
                 $query->bindValue(4, $reply_to);                 

                 try {
                   $query->execute();            
                 }

                 catch(PDOException $e) {
                    die($e->getMessage());
                 }  
                 header('Location:home.php?success');
                 exit();                 
             }
          } 

          if (isset($_GET['success']) && empty($_GET['success'])) 
          { 
              header('Location:home.php');
              echo '<span class="error">Report is succesfully created</span>';  
          }

          ?>

          <form action="" method="POST" accept-charset="UTF-8">
              <div style="font-weight:bold;padding:17px 80px;text-decoration:underline;">Section A</div>
              <table class="create_report">                           
                <tr><td><label>Report Name</label><span style="color:#A60000">*</span></td>
                    <td><input type="text" name="report_name" required placeholder="Name of the report" value="<?php if(isset($_POST["report_name"])) echo $report_name; ?>" size="30" maxlength="30">             
                </td></tr>              

                  <tr><td><label>From</label><span style="color:#A60000">*</span></td>
                      <td><input type="text" name="from_address" required placeholder="From address" value="<?php if(isset($_POST["from_address"])) echo $from_address; ?>" size="30">             
                  </td></tr>

                  <tr><td><label>Subject</label><span style="color:#A60000">*</span></td>
                      <td><input type="text" name="subject" required placeholder="Subject" value="<?php if(isset($_POST["subject"])) echo $subject; ?>" size="30">             
                  </td></tr>

                  <tr><td><label>Reply To</label><span style="color:#A60000">*</span></td>
                      <td><input type="text" name="reply_to" required placeholder="Reply address" value="<?php if(isset($_POST["reply_to"])) echo $reply_to; ?>" size="30">            
                  </td></tr>

               <tr><td><input type="submit" value="create report" style="background:#8AC007;color:#080808;padding:6px;" name="create_es_report"></td></tr> 
             </table>                       
          </form>

          <?php
            //IF THERE ARE ERRORS, THEY WOULD BE DISPLAY HERE
            if (empty($errors) === false) {
               echo '<div>' . implode('</p><p>', $errors) . '</div>';
            }                  
      }  


      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[] = '<span class="error">All fields are required.</span>';
             }
             else
             {
                if (isset($_POST['report_name']) && empty($_POST['report_name'])) { $errors[] = '<span class="error">Report Name is required</span>'; }
                else if (!ctype_alnum($_POST['report_name']))
                {  $errors[] = '<span class="error">Report Name: Whitespace is not allowed, only alphabets and numbers are required</span>';  }

                if (isset($_POST['to_address']) && empty($_POST['to_address'])) 
                { $errors[] = '<span class="error">to address is required</span>'; }
                else if (filter_var($_POST['to_address'], FILTER_VALIDATE_EMAIL) === false)
                { $errors[] = '<span class="error">Please enter a valid to address</span>';  }

                if (isset($_POST['subject']) && empty($_POST['subject'])) { $errors[] = '<span class="error">Subject is required</span>'; }
                else if (!ctype_alnum($_POST['subject']))
                {  $errors[] = '<span class="error">Subject: Whitespace is not allowed, only alphabets and numbers are required</span>';  }

                if (isset($_POST['reply_to']) && empty($_POST['reply_to'])) { $errors[] = '<span class="error">Reply To is required</span>'; }
                else if (filter_var($_POST['reply_to'], FILTER_VALIDATE_EMAIL) === false)
                { $errors[] = '<span class="error">Please enter a valid Reply-To address</span>';  }                        
             }

             if (empty($errors) === true)
             {               
                 $query = $this->db->prepare("INSERT INTO report(report_name, to_address, subject, reply_to) VALUES (?, ?, ?, ?) ");

                 $query->bindValue(1, $report_name);                
                 $query->bindValue(2, $to_address);
                 $query->bindValue(3, $subject);         
                 $query->bindValue(4, $reply_to);                 

                 try {
                   $query->execute();            
                 }

                 catch(PDOException $e) {
                    die($e->getMessage());
                 }  
                 header('Location:home.php?success');
                 exit();                 
             }
          } 

          if (isset($_GET['success']) && empty($_GET['success'])) 
          { 
              header('Location:home.php');
              echo '<span class="error">Report is succesfully created</span>';  
          }

          ?>

          <form action="" method="POST" accept-charset="UTF-8">
              <div style="font-weight:bold;padding:17px 80px;text-decoration:underline;">Section A</div>
              <table class="create_report">                           
                <tr><td><label>Report Name</label><span style="color:#A60000">*</span></td>
                    <td><input type="text" name="report_name" required placeholder="Name of the report" value="<?php if(isset($_POST["report_name"])) echo $report_name; ?>" size="30" maxlength="30">             
                </td></tr>              

                  <tr><td><label>to</label><span style="color:#A60000">*</span></td>
                      <td><input type="text" name="to_address" required placeholder="to address" value="<?php if(isset($_POST["to_address"])) echo $to_address; ?>" size="30">             
                  </td></tr>

                  <tr><td><label>Subject</label><span style="color:#A60000">*</span></td>
                      <td><input type="text" name="subject" required placeholder="Subject" value="<?php if(isset($_POST["subject"])) echo $subject; ?>" size="30">             
                  </td></tr>

                  <tr><td><label>Reply To</label><span style="color:#A60000">*</span></td>
                      <td><input type="text" name="reply_to" required placeholder="Reply address" value="<?php if(isset($_POST["reply_to"])) echo $reply_to; ?>" size="30">            
                  </td></tr>

               <tr><td><input type="submit" value="create report" style="background:#8AC007;color:#080808;padding:6px;" name="create_rp_report"></td></tr> 
             </table>                       
          </form>

          <?php
            //IF THERE ARE ERRORS, THEY WOULD BE DISPLAY HERE
            if (empty($errors) === false) {
               echo '<div>' . implode('</p><p>', $errors) . '</div>';
            }                  
      }   

   }//Report CLASS ENDS   

1 个答案:

答案 0 :(得分:0)

首先 1)

您需要javascript代码来处理此表单验证,如:

$("form").submit(function(){
    // Your validation code
    if(validation not satisfied){
        return false;
    }
});

2)检查表格是否有效并在提交后激活该表格。你的HTML应该喜欢这个

<body>
 <div class="container2">
       <div style="margin:0px 0px;padding:3px 217px;overflow:hidden;">
         <div id="cs" style="float:left;margin:0px 0px;padding:7px;"><input type="button" value="CREATE CS"></div>
         <div id="rp" style="float:left;margin:0px 0px;padding:7px;"><input type="button" value="CREATE RP"></div><br>
       </div>
       <?php $active_form = (is_first_form_checked)?'first_form':'second_form'; ?>
       <div id="cs_content" style="<?php echo ($active_form == 'first_form')?'display:none;':''; ?>"> <?php $report->create_cs_report(); ?> </div>            
       <div id="rp_content" style="<?php echo ($active_form == 'second_form')?'display:none;':''; ?>"> <?php $report->create_rp_report(); ?> </div>        
 </div>