【问题标题】:Ajax, PHP and MySQL and append data in a pageAjax、PHP 和 MySQL 以及在页面中附加数据
【发布时间】:2017-01-28 10:35:51
【问题描述】:

我正在构建一个如下所示的页面:

目标是什么

  • 当用户单击输入框并选择特定日期时,它会运行一个 php 脚本,在数据库中搜索具有该特定日期的记录并将它们附加到特定的 div 中。

到目前为止我做了什么:

  1. HTML 和 CSS:
<div class="input-group col-md-8 col-sm-8 col-xs-8">
      <input type="text" class="form-control date">
      <span class="input-group-addon calendar-date-picker">
           <i class="glyphicon glyphicon-calendar"></i>
      </span>
  </div>

(...)

this is cool :<p> <?php print_r($_POST);?> </p>
<div class="results-ajax"></div>

它使用输入类中的“日期”来打开日期选择器日历

它有一个特定的 div,其中包含一个类“results-ajax”,脚本生成的输入应显示在

  1. JS 和 Ajax:
jQuery(function($) {

  $(".date").datepicker({
    onSelect: function(dateText) {
      display("Selected date: " + dateText + "; input's current value: " + this.value);
      $(this).change();
    }
  }).on("change", function() {
    display("Got change event from field");
    $.ajax({
      type: "POST",
      url: 'events_script.php',
      data: ({dates: this.value}),
      success: function(data) {
        $(".results-ajax").load(data);
        alert(data);
      }
  });
});
  function display(msg) {
    $("<p>").html(msg).appendTo(document.body);
  }

});

一旦选择了新的日期,结果将显示在下面的结果下。 .

Ajax 调用在 events_script.php 中发布

  1. PHP (events_script.php)
<?php
include 'config/config.php';
include 'libraries/database.php';

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    print_r($_POST);
    echo $_POST[dates];
    $dias= $_POST[dates];

//进行数据库查询

$sql = "SELECT *
        FROM events
        RIGHT OUTER JOIN companies ON companies.companyID = events.Ref_ID
        WHERE events.Start_Date= '$dias%'
        ORDER BY events.Start_Date DESC";

$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {

//输出每一行的数据

  while ($row = mysqli_fetch_assoc($result)) {
      echo '<p>'.print_r($_POST).'</p>';
  }

} else { echo 'No results found.'; } } ?&gt;

结果

1. JS显示和追加的结果

2. Ajax 中的警报结果(使用 PHP 脚本):

问题:

  1. 为什么警告数据没有按照 Ajax 中的命令加载到具有“results-ajax”类的 div 中?

我也使用 appendTo 而不是 load 进行了测试,但也没有用。

还有,

  1. 。为了使发布的数据在查询中被数据库识别一次('$dias%'),该过程中是否有任何步骤缺失/错误?

因为 rn 显示“未找到结果”(如您在警报中看到的那样),并且数据库中有特定日期的记录。

(发生了很多次,该值与数据库中的值完全相同,但因为一个是字符串,另一个是日期,所以没有显示任何内容 - 只是为了确保这不是其中一种情况)

注意:

PHP 错误日志中没有错误。

在控制台中,出现以下错误:GET example.com/Array( 404 (Not Found)。一旦关闭警报框就会出现。

【问题讨论】:

  • 您确定您的 php 脚本正在获取发布日期吗?
  • ok,$('.results-ajax').html(data) 解决了第一个问题
  • @tiagoperes 确保您的 $dias 将相同格式与表中的日期列进行比较,可能您的日期列与您的 $dias 格式不同,日期选择器具有不同的默认格式
  • @Shubhranshu 是的,在警告框中可以看到
  • 在你的 php 脚本中就像$newdate = date('Y-m-d h:m:s',strtotime($dias)); 然后在你的查询中使用$newdate

标签: javascript php jquery mysql ajax


【解决方案1】:

您需要将日期转换为 MySql 格式,方法如下:

function date_convert($value,$fromFormat,$toFormat){
    try{
        $myDateTime = DateTime::createFromFormat($fromFormat, $value);
        if ($myDateTime)
            return  $myDateTime->format($toFormat);
    } catch (Exception $ex) {
        return '';
    }
}
// this supposes that by 01/03/2017 you're talking about the 1st of Mars
// if you mean the 3rd or January change the from format to : 'm/d/Y'
$dias = date_convert($dias,'d/m/Y','Y-m-d'); 

【讨论】:

  • 确保在发出数据库请求之前将日期转换为 YYYY-MM-DD
  • 即使在数据库请求之前添加也不起作用
【解决方案2】:

我使用以下代码让它工作

  1. HTML 和 CSS:
<div class="input-group col-md-8 col-sm-8 col-xs-8">
      <input type="text" class="form-control date">
      <span class="input-group-addon calendar-date-picker">
           <i class="glyphicon glyphicon-calendar"></i>
      </span>
  </div>

(...)

this is cool :<p> <?php print_r($_POST);?> </p>
<div class="results-ajax"></div>

它使用输入类中的“日期”来打开日期选择器日历

它有一个特定的 div,其中包含一个类“results-ajax”,其中脚本生成的输入显示在

  1. JS 和 Ajax:
jQuery(function($) {

  $(".date").datepicker({
    onSelect: function(dateText) {
      display("Selected date: " + dateText + "; input's current value: " + this.value);
      $(this).change();
    }
  }).on("change", function() {
    display("Got change event from field");
    $.ajax({
      type: "POST",
      url: 'events_script.php',
      data: ({dates: this.value}),
      success: function(data) {
        $('.results-ajax').html(data);
        alert(data);
      }
  });
});
  function display(msg) {
    $("<p>").html(msg).appendTo(document.body);
  }

});

一旦选择了新的日期,结果将显示在结果下方。 .

Ajax 调用在 events_script.php 中发布

  1. PHP (events_script.php)
<?php
include 'config/config.php';
include 'libraries/database.php';

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    print_r($_POST);
    echo $_POST[dates];
    $dias= $_POST[dates];
    $mysql_date = date('Y-m-d', strtotime($dias));

//进行数据库查询

$sql = "SELECT *
        FROM events
        left JOIN companies ON companies.companyID = events.Ref_ID AND companies.Ref_Type = events.Ref_Type
        WHERE events.Start_Date= '".$mysql_date."'
        ORDER BY events.Start_Date DESC";

$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {

//输出每一行的数据

  while ($row = mysqli_fetch_assoc($result)) {
      echo '<p>It works!</p>';
  }

} else { echo 'No results found.'; } } ?&gt;

结果

1. Ajax 中的警报结果(使用 PHP 脚本):

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    • 2011-03-25
    • 2018-12-30
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多