Javascript Ajax、PHP和MySQL,并在页面中添加数据
我正在构建一个如下所示的页面: 目标是什么Javascript Ajax、PHP和MySQL,并在页面中添加数据,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,我正在构建一个如下所示的页面: 目标是什么 当用户在输入框中单击并选择特定日期时,它会运行一个php脚本,在数据库中搜索具有该特定日期的记录,并将它们附加到特定的div中 到目前为止我做了什么: HTML和CSS: //每行的输出数据 while ($row = mysqli_fetch_assoc($result)) { echo '<p>'.print_r($_POST).'</p>'; } while ($row = mysqli_f
- 当用户在输入框中单击并选择特定日期时,它会运行一个php脚本,在数据库中搜索具有该特定日期的记录,并将它们附加到特定的div中
while ($row = mysqli_fetch_assoc($result)) {
echo '<p>'.print_r($_POST).'</p>';
}
while ($row = mysqli_fetch_assoc($result)) {
echo '<p>It works!</p>';
}
while($row=mysqli\u fetch\u assoc($result)){
回显“”。打印($\u POST)。“”;
}
}其他{echo'未找到结果。;}?>
结果:
1.JS显示和附加的结果
2.Ajax中警报的结果(使用PHP脚本):
问题:
在控制台中,出现以下错误:GET example.com/Array(404(未找到)。关闭警报框后会显示该错误。您需要将日期转换为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');
我用下面的代码让它工作
while ($row = mysqli_fetch_assoc($result)) {
echo '<p>'.print_r($_POST).'</p>';
}
while ($row = mysqli_fetch_assoc($result)) {
echo '<p>It works!</p>';
}
while($row=mysqli\u fetch\u assoc($result)){
回音“它能工作!”;
}
}其他{echo'未找到结果。;}?>
结果:
1。Ajax中警报的结果(使用PHP脚本):您确定您的php脚本正在获取发布日期吗?好的,$('.results ajax').html(数据)解决了第一个问题problem@tiagoperes确保您的
$dias
与表中的日期列比较的格式相同,可能是日期列的格式与$dias
日期选择器的默认格式不同format@Shubhranshu是的,它可以在php脚本的警报框中看到,就像$new一样date=date('Y-m-d h:m:s',strotime($dias));
然后在查询中使用$newdate
,请确保在进行数据库请求之前将日期转换为YYYY-MM-DD,即使在数据库请求之前添加该日期也不起作用
while ($row = mysqli_fetch_assoc($result)) {
echo '<p>'.print_r($_POST).'</p>';
}
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');
<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>
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);
}
});
<?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>';
}