Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to create two reports and submit the report data to database by using two functions within a class: Here I have two submit buttons: "Create ES Report" and "Create RP Report".

(1) When I click on "Create ES Report", create_es_report form should display and be able to fill the data and submit successfully to database and if errors it should display the errors on the same div.

(2) When I click on "Create RP Report", create_rp_report form should display and be able to fill the data and submit successfully to dataabase and if errors it should display the errors on the same div.

Rightnow, When I click on any of the submit buttons, nothing was displaying

index.php

<html>
 <head>      
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script type="text/javascript">

   $(document).ready(function(){ 

      $('#es').click(function () 
      {
         create();
      });      

   });

function create(){

     $.ajax({  
      url: "check.php?proc=create",
      type: "POST",  
      dataType:'json',
      success: function(data)
      {  
             $('#returnMessage').show();
             $('#returnMessage').html(data.mes);
      }


   });  

return false;
}    

  </script>

 </head>

 <body>

<div class="container2">        
  <div style="float:left;margin:0px 0px;padding:7px;"><input type="submit" value="Create ES Report" id="es"></div>
  <div id="returnMessage" style="display:none;"></div>        
</div>   
 </body>
</html>

check.php

<?php
 require 'includes/config.inc.php';
 require 'classes/class.report.php';
 $report = new Report($db); 

 if(isset($_GET['proc']) && !empty($_GET['proc']))
 {
    $proc =  $_GET['proc'];
    if($proc == 'create')
    {
       $report->create_es_report();
       $return = array('mes' => 'Created' );
       header('content-type: application/json; charset=utf-8');
       echo json_encode($return);
    }

 }
 else
 {
     $return = array('mes' => 'The $_GET is empty , check if all parms and ajax function passing to the true file, good luck :).' );
     header('content-type: application/json; charset=utf-8');
     echo json_encode($return);
 }

?>

class.report.php

<?php
    class Report
    {
       private $db;

       public function __construct($database){
          $this->db = $database;
       }

       //CREATE DATASOURCE REPORT
       public function create_es_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>';
            }                  
      }   

   }//Report CLASS ENDS   
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
131 views
Welcome To Ask or Share your Answers For Others

1 Answer

My guess is that your PHP is failing and the success option is not triggering.

I would suggest adding a console write of data.res in your success option and also add an error option and add a complete option that will write something different to console so you can determine if jquery is failing or if php is failing.

As a side note, I would combine your create_es and create_rp function to 1 since they are identical except for the query string value being passed in ajax. You would then call create_report("es") and create_report("rp") in your click events and your ajax url would be "check.php?proc=" + report, where report is your function param.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...