【问题标题】:Getting value from datepicker and assign to php从 datepicker 获取值并分配给 php
【发布时间】:2020-02-02 15:02:00
【问题描述】:

我有一个用于预约的表格,其中包含 1 个输入和 2 个选择: First:使用可以选择日期的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 的结果来填充选择)

【问题讨论】:

    标签: javascript php jquery html


    【解决方案1】:

    每次用户选择日期时,您都需要创建一个AJAX request。为此,您可以使用 datepicker 库的 onSelect` event

    1. 首先让我们摆脱 timesquery.phppopulatelisttimes.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">
    
    1. 现在让我们在您的 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.')
            });
        }
    });
    
    1. 最近,让我们打印出timesquery.php 文件中的选项HTML,我相信您的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';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多