javascript - 从 datepicker 获取值并分配给 php
问题描述
我有一个带有 1 个输入和 2 个选择的预约表格: 首先:使用 jQuery 输入可以选择一个日期。 第二:选择与美发列表(不相关)。 第三:选择基于日期的可用时间列表。
我一直根据日期进入数据库,所以我的目标是:
当用户选择一个日期时,我的查询会使用可用时间列表填充第三个选择,因此我的查询需要他选择的日期。如何获取所选日期的值并在 php 中使用它进行查询?
HTML/视图:
<h1>Book Reservation</h1>
<b><p>Select Date:</p></b>
<input id="datepicker" name="date" width="270" style = "display: block; float: center;" disabled required>
<script>
$('#datepicker').datepicker({
uiLibrary: 'bootstrap'
});
</script>
<?php include "../../Models/appointment/hdquery.php"?>
<b><p style = "margin-top: 10px; ">Select Hair Dressing :</p></b>
<select class="select-css" name = "hairdressing">
<?php include "../../Models/appointment/populatelisthd.php"?>
</select>
<b><p style = "margin-top: 10px;">Select Time:</p></b>
<?php include "../../Models/appointment/timesquery.php"?>
<select class="select-css" name = "times"><
<?php include "../../Models/appointment/populatelisttimes.php"?>
</select><p style="margin-top: 10px;">
<input type="submit" value = "Confirm">
包含中的 hdquery 和 populatelisthd 与我的问题无关,所以我不会显示它们。
时间查询:
<?php
$mysqli = NEW MySQLi('localhost','root','','cappeli');
$result = $mysqli->query("SELECT t.`hour` FROM `times` t LEFT JOIN ( SELECT `time` FROM `reservations` WHERE `date` = '$date' GROUP BY `time` HAVING COUNT(*) > 2) r on r.time = t.`hour` WHERE r.time IS NULL ");
?>
如果您在查询中注意到有一个WHERE date=$date
我想从 datepicker 的输入字段中获取此日期,并且在每次更改时我都需要查询来更新时间。我怎么做?(PS:我有另一个 php 文件来填充它,但我需要结果timesquery
来填充 select )
解决方案
每次用户选择日期时,您都需要发出AJAX 请求。为此,您可以使用 datepicker 库的onSelect` 事件。
- 首先让我们摆脱
timesquery.php
和populatelisttimes.php
文件:
<?php include "../../Models/appointment/hdquery.php"?>
<b><p style = "margin-top: 10px; ">Select Hair Dressing :</p></b>
<select class="select-css" name = "hairdressing">
<?php include "../../Models/appointment/populatelisthd.php"?>
</select>
<b><p style = "margin-top: 10px;">Select Time:</p></b>
<!-- -->
<select class="select-css" name = "times">
<!-- Options will be added here dynamically using jQuery -->
</select><p style="margin-top: 10px;">
<input type="submit" value = "Confirm">
- 现在让我们听听jQuery中的日期“选择” :
$('#datepicker').datepicker({
onSelect: function (date) {
$.ajax({
type: "POST",
url: 'timesquery.php',
data: {
'date': date,
},
}).done(function (data) {
$('select[name=times]').html(data);
}).fail(function () {
alert('An error has occured.')
});
}
});
- 最近,让我们打印出文件中的选项 HTML
timesquery.php
,我相信您的populatelisttimes.php
文件会负责打印选项,所以我只包含它:
$date = $_POST['date']; // <====== Here is our date variable, you need to escape this!!!
$mysqli = NEW MySQLi('localhost','root','','cappeli');
$result = $mysqli->query("SELECT t.`hour` FROM `times` t LEFT JOIN ( SELECT `time` FROM `reservations` WHERE `date` = '$date' GROUP BY `time` HAVING COUNT(*) > 2) r on r.time = t.`hour` WHERE r.time IS NULL ");
include __DIR__ . '/populatelisttimes.php';
推荐阅读
- ios - 无法更改 UITableView 的 tableHeaderView 的颜色。(不是章节标题!)
- angular - 删除 angular slickgrid 的每个列标题中的隐藏列选项
- sql - 如何在 PRESTO 中将日期转换为日期字符串
- javascript - 如何处理使用代码拆分的 Docker webpack 应用程序的新版本部署?
- sql - 将“输入”和“输出”的几条记录分组到“会话”中。通过 SQL 查询或编程
- reactjs - 在另一个字段中调用 form.change(fieldName, value) 不会改变 fieldName 字段
- java - JMockit with JUnit5 - JMockit 没有被初始化
- reactjs - 如何使用 react-use-fuse?
- typescript - 你如何在打字稿中将日期时间'00:00 pm'转换为'00:00:00'
- angular - 构建表单时控件和 AbstractControls 出错