php - 创建具有显示/隐藏功能的 PHP 表单

标签 php jquery

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

现在,我的表单工作正常,我可以成功插入数据,但问题是当我填写表单数据后单击提交时,内容隐藏并显示第一个 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)

您需要 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>

关于php - 创建具有显示/隐藏功能的 PHP 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24153882/

相关文章:

php - 一个 PHP 函数,用于在 true/false 上插入或更新 mysql 表

jquery - Jquery 中的 XML 解析

jquery - 在注入(inject)之前拖动新的 DOM 元素并将其他元素推开

Jquery点击功能只有在第二次点击后才起作用

javascript - 为 ajax 弹出窗口发送文本值

php - 选择在包含多个以逗号分隔的 id 的列中搜索某个 id 的行

php - 使用 php 输出原始 XML

jquery - 为焦点添加延迟 jquery

jquery - 每个循环到(for 循环或 while 循环)

php - CSS,圆 Angular 不起作用