Home PHP MySQL Ajax form data output
Reply: 0

PHP MySQL Ajax form data output

user7241
1#
user7241 Published in April 26, 2018, 1:42 pm

I have a code that I have been trying to run for days without success, could anyone look at it and help figure where I am going crazy?

here is report.js:

$(document).ready(function() {
$('#navReport').addClass('active');
// order date picker
$('#startDate').datepicker();
// order date picker
$('#endDate').datepicker();

$('#getReportForm')
.unbind('submit')
.bind('submit', function() {
  var startDate = $('#startDate').val();
  var endDate = $('#endDate').val();
  var personId = $('#personId').val();

  if (startDate == '' || endDate == '' || personId == '') {
    if (startDate == '') {
      $('#startDate')
        .closest('.form-group')
        .addClass('has-error');
      $('#startDate').after(
        '<p class="text-danger">The Start Date is required</p>'
      );
    } else {
      $('.form-group').removeClass('has-error');
      $('.text-danger').remove();
    }

    if (endDate == '') {
      $('#endDate')
        .closest('.form-group')
        .addClass('has-error');
      $('#endDate').after(
        '<p class="text-danger">The End Date is required</p>'
      );
    } else {
      $('.form-group').removeClass('has-error');
      $('.text-danger').remove();
    }

    if (personId == '') {
      $('#personId')
        .closest('.form-group')
        .addClass('has-error');
      $('#personId').after(
        '<p class="text-danger">Person Name is required</p>'
      );
    } else {
      $('.form-group').removeClass('has-error');
      $('.text-danger').remove();
    }
  } else {
    $('.form-group').removeClass('has-error');
    $('.text-danger').remove();

    var form = $(this);
    $.ajax({
      url: form.attr('action'),
      type: form.attr('method'),
      data: form.serialize(),
      dataType: 'text',
      success: function(response) {
        var mywindow = window.open(
          '',
          'Child Behavior Management System',
          'height=400,width=600'
        );
        mywindow.document.write('<html><head><title>Report</title>');
        mywindow.document.write('</head><body>');
        mywindow.document.write(response);
        mywindow.document.write('</body></html>');

        mywindow.document.close(); // necessary for IE >= 10
        mywindow.focus(); // necessary for IE >= 10

        mywindow.print();
        mywindow.close();
      } // /success
    }); // /ajax
  } // /else
  console.log(personId, startDate, endDate);
  return false;
});
});    

Here is getReport.php:

<?php
require_once 'core.php';

if($_POST) {

$personId = $_POST['personId'];
$startDate = $_POST['startDate'];
$date = DateTime::createFromFormat('m/d/Y',$startDate);
$start_date = $date->format("Y-m-d");

$endDate = $_POST['endDate'];
$format = DateTime::createFromFormat('m/d/Y',$endDate);
$end_date = $format->format("Y-m-d");

$table = '';
$sql = "SELECT notes.note_id, notes.note_content, notes.person_id,
                notes.note_date, notes.note_status, persons.persons_name
                FROM notes INNER JOIN persons ON notes.person_id = persons.persons_id
                WHERE notes.person_id = '$personId' AND notes.note_date
                BETWEEN CAST('$start_date' AS DATE) AND CAST('$end_date' AS DATE)
                AND notes.note_status = 1"

$query = $connect->query($sql);

$table = '
<table border="1" cellspacing="0" cellpadding="0" style="width:100%;">
    <tr>
        <th>Date</th>
        <th>Note</th>
        <th>Resident</th>
    </tr>

    <tr>';
    while ($result = $query->fetch_assoc()) {
        $table .= '<tr>
            <td><center>'.$result['notes.note_date'].'</center></td>
            <td><center>'.$result['notes.note_content'].'</center></td>
            <td><center>'.$result['persons.persons_name'].'</center></td>

        </tr>';
    }
    $table .= '
    </tr>
</table>
';

echo $table;

}
?>

And here is report.php:

<?php require_once 'includes/header.php'; ?>
<div class="row">
<div class="col-md-12">
    <div class="panel panel-default">
        <div class="panel-heading">
            <i class="glyphicon glyphicon-check"></i>   Generate Report
        </div>
        <!-- /panel-heading -->
        <div class="panel-body">
            <form class="form-horizontal" action="php_action/getReport.php" method="post" id="getReportForm">
                <div class="form-group">
            <label for="personId" class="col-sm-2 control-label">Resident</label>
                <div class="col-sm-10">
                  <select type="text" class="form-control" id="personId" placeholder="Resident" name="personId" >
                    <option value="">~~SELECT~~</option>
                    <?php
                    $sql = "SELECT persons_id, persons_name, persons_status FROM persons WHERE persons_status = 1";
                            $result = $connect->query($sql);
                            while($row = $result->fetch_array()) {
                                echo "<option value='".$row[0]."'>".$row[1]."</option>";
                            } // while ?>
                  </select>
                </div>
        </div> <!-- /form-group-->
                <div class="form-group">
                <label for="startDate" class="col-sm-2 control-label">Start Date</label>
                <div class="col-sm-10">
                  <input type="text" class="form-control" id="startDate" name="startDate" placeholder="Start Date" />
                </div>
              </div>
              <div class="form-group">
                <label for="endDate" class="col-sm-2 control-label">End Date</label>
                <div class="col-sm-10">
                  <input type="text" class="form-control" id="endDate" name="endDate" placeholder="End Date" />
                </div>
              </div>
              <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                  <button type="submit" class="btn btn-success" id="generateReportBtn"> <i class="glyphicon glyphicon-ok-sign"></i> 
Generate Report</button>
                </div>
              </div>
            </form>
        </div>
        <!-- /panel-body -->
    </div>
</div>
<!-- /col-dm-12 -->
</div>
<!-- /row -->
<script src="custom/js/report.js"></script>
<?php require_once 'includes/footer.php'; ?>

I would love to have expert solution to this. I have looked for solution almost the entire night but no joy. The popup is not firing when I select the preferred person, start and end dates based on the query.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.298846 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO