Ajax,PHP和MySQL并在页面中附加数据 [英] Ajax, PHP and MySQL and append data in a page
本文介绍了Ajax,PHP和MySQL并在页面中附加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在建立一个如下所示的页面:
I'm building a page that looks like the following:
什么是目标:
- 当用户单击输入框并选择一个特定日期时,它将运行一个php脚本,该脚本在数据库中搜索具有该特定日期的记录并将它们附加到特定的div中.
到目前为止我做了什么:
What have I done so far:
- 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>
. 它在输入类中使用日期"来打开日期选择器日历.
. 它具有一个带有"results-ajax"类的特定div,其中脚本生成的输入显示在上.
- 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 中.
- 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.'; } } ?>
结果:
1..Ajax中的警报结果(使用PHP脚本):
1. The result of the alert in Ajax (using the PHP script):
感谢您的帮助!它正在使用此处提到的代码进行工作
推荐答案
您需要将日期转换为MySql格式,以下是操作方法:
You need to convert the date to MySql format, here is how to do it :
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');
这篇关于Ajax,PHP和MySQL并在页面中附加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文