Ajax,PHP和MySQL,并在页面中附加数据

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

事件

目标是什么:

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

到目前为止我做了什么:

  1. HTML和CSS:

(……)

 this is cool :

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

它有一个特定的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) { $("

").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('Ym-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 '

It works!

'; }

} else { echo 'No results found.'; } } ?>

结果

1. Ajax中警报的结果(使用PHP脚本):
在此处输入图像描述

感谢您的帮助! 它正在使用这里提到的代码

您需要将日期转换为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','Ym-d');