Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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   
share|improve this question
    
Any messages in the console? –  jeroen Jun 10 at 14:08
    
No Iamn't getting any errors/messages, When I run, and click the submit buttons, it displays nothing –  ronquiq Jun 10 at 14:09
1  
You should try to narrow it down, this is too much to go through. –  jeroen Jun 10 at 14:12
1  
Why do you send url: "check.php?proc=create_es", ? it's should be url: "check.php?proc=create", look how GET works at check.php , change all the others too. –  Ravg Jun 10 at 14:17
1  
You are sending proc=create_es but checking if($proc == 'create')? Also, you are not sending any data in your post, so if (isset($_POST['create_es_report'])) will return false as well. –  Sean Jun 10 at 14:18

2 Answers 2

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.

share|improve this answer

You seems don't know how to handle PHP and AJAX as well.

First change the urls E.G:

   url: "check.php?proc=create_es",

to

   url: "check.php?proc=create",

Look how at check.php the GET works.

And change type: "POST", to type: "GET",

Now to return the error's it's more complicated from just call a php function.

To return the error's you return from the create_es_report error's to the check.php file and return json format to you'r html page, this why i said LEARN ajax first.

Also don't use htmlentities i suggest you to use HTMLPURIFER to santize inputs from malicious inputs.

share|improve this answer
    
I have edited according to your suggestion with only one submit button "Create ES Report" –  ronquiq Jun 10 at 14:30
    
Ok sure.. I will go with htmlpurifier, Thank You –  ronquiq Jun 10 at 14:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.