PHP MySQL Ajax表单数据输出

时间:2018-04-17 06:22:57

标签: php mysql ajax

我有一个代码,我一直试图运行几天没有成功,有人可以看看它,并帮助我找到疯狂的地方吗?

这里是 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;
});
});    

这是 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;

}
?>

这是 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'; ?>

我很乐意为此提供专家解决方案。我几乎整个晚上都在寻找解决方案,但没有快乐。当我根据查询选择首选人员,开始和结束日期时,弹出窗口不会触发。

0 个答案:

没有答案