【问题标题】:PHP Form with jQuery DatePicker带有 jQ​​uery DatePicker 的 PHP 表单
【发布时间】:2018-02-20 05:35:03
【问题描述】:

我正在使用 MySQL 构建一个 MVC PHP Web 应用程序。我正在使用表单从用户那里收集信息。当日期信息以正确的顺序输入时,日期输入起作用,但我希望这对我不太懂技术的同事(上帝保佑他们)更加用户友好。

这是我在 'index.php' 中的表单:

    if($action == 'add_order'){
  $distributor_id = filter_input(INPUT_POST, 'distributor_id', FILTER_VALIDATE_INT);
  $code = filter_input(INPUT_POST, 'code');
  $name = filter_input(INPUT_POST, 'name');
  $date = filter_input(INPUT_POST, 'date');
  if($distributor_id == NULL || $distributor_id == FALSE || $code == NULL ||
      $name == NULL || $date ==NULL){
        $error = "Invalid order data: Something is wrong";
        include('../errors/error.php');
      }else{
        add_order($distributor_id, $code, $name, $date);
        header("Location: .?distributor_id=$distributor_id");
      }
}
?>

这是我在 'order_add.php' 中的表单:

    <h1>Add order</h1>
  <form action="index.php" method="post" id="add_order_form">
    <input type="hidden" name="action" value="add_order">
    <label>distributor:</label>
    <select name="distributor_id">
      <?php foreach ($distributors as $distributor) : ?>
        <option value="<?php echo $distributor['distributorID']; ?>">
            <?php echo $distributor['distributorName']; ?>
        </option>
      <?php endforeach; ?>
    </select>
    <br>
    <label>Order Code:</label>
    <input type="text" name="code">
    <br>
    <label>Customer Name:</label>
    <input type="text" name="name">
    <br>
    <label>Order Date:</label>
    <input type="text" id="date" name="date">
    <br>
    <label>&nbsp;</label>
    <input type="submit" value="Add order">
    <br>
  </form>
  <p class="last_paragraph">
    <a href="index.php?action=list_orders">View order List</a>
  </p>
</main>

这里是视图'header_two_date_picker.php':

<!DOCTYPE html> 
<!-- This header page is going to be used with my Order Schedule PHP Program and main_two.css-->
<html>
  <!-- This is the body section-->
  <head>
    <title>Order Status</title>
      <link rel='stylesheet' type='text/css' href = "../main_two.css">

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
          <title>jQuery UI Datepicker - Default functionality</title>
          <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
          <link rel="stylesheet" href="/resources/demos/style.css">
          <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
          <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
          <script>

          $( function(){
            $( "#date" ).datepicker();
          } );
          </script>

  </head>
  <!-- This is the body section-->
  <body>

  <header>
    <h1>My PHP Sandbox - Date Picker</h1>

  </header>

这是我的模型“order_db.php”

    function add_order($distributor_id, $code, $name, $date){
  global $db;
  $query = 'INSERT INTO orders
                (distributorID, orderCode, orderName, orderDate)
            VALUES
                (:distributor_id, :code, :name, :date)';
  $statement = $db->prepare($query);
  $statement->bindValue(':distributor_id', $distributor_id);
  $statement->bindValue(':code', $code);
  $statement->bindValue(':name', $name);
  $statement->bindValue(':date', $date);
  $statement->execute();
  $statement->closeCursor();
}
?>

jQuery DatePicker 显示甚至填充表单字段,但我的数据库中的表显示“0000-00-00”。

我的问题是:如何获取 DatePicker 数据,并将其转换为 MySql 将存储的日期格式?

感谢您抽出宝贵时间查看我的问题。

这是我的“order_list.php”文件的一部分

<!-- Display table of orders -->
  <h2><?php echo $distributor_name; ?></h2>
  <table>
    <tr>
      <th>Order Code</th>
      <th>Order Name</th>
      <th>Order Date</th>
      <th>&nbsp;</th>
    </tr>
    <?php foreach ($orders as $order) : ?>
      <tr>
        <td><?php echo $order['orderCode']; ?></td>
        <td><?php echo $order['orderName'] ?></td>
        <td><?php echo $order['orderDate'] ?></td>
        <td><form action="." method="post">
          <input type="hidden" name="action" value="delete_order">
          <input type="hidden" name="order_id" value="<?php echo $order['orderID']; ?>">
          <input type="hidden" name="distributor_id" value="<?php echo $order['distributorID']; ?>">
          <input type="submit" value="Delete">
        </form></td>
      </tr>
    <?php endforeach; ?>
  </table>
  <p class = "last_paragraph">
    <a href="?action=show_add_form">Add order</a>
  </p>

    ***<h2>This is a test to output $date: <?php echo $date; ?></h2>
    <h2>This is a test to output echo $_POST['date']: <?php echo*** $_POST['date']; ?></h2>

If you can look past the heinous styling of this page you will see the tests where i tried to echo the variables are not displaying anything.

【问题讨论】:

  • 您是否尝试过打印以筛选您在数据库中插入的值?也许$date 变量没有你认为的那样。

标签: php jquery mysql model-view-controller datepicker


【解决方案1】:

你好 Buhlahkay。

  • 确保您使用的数据库结构日期为使用日期格式.. mysql 的日期字段被接受2017-12-30 (Year-Month-Date)

  • 调试你自己的代码输出echo $_POST['date'];并查看其格式化数据库是否接受格式(年-月-日)的日期。

  • 将日期选择器 Jquery 修改为 $('#date').datepicker({dateFormat : 'yy-mm-dd'});

  • 如果它显示格式没有问题..请重新正确更新代码,因为 index.php 正在做的事情就像一些更详细的 1 比 1. 以避免混淆..

【讨论】:

  • Ajmal 您推荐的日期选择器 Jquery 修改(第三个要点)解决了我的问题,现在日期从日期选择器发送到数据库。非常感谢!
  • 您提到重新上传代码。您希望我在哪里这样做?
  • @Buhlahkay 将$( "#date" ).datepicker();修改为$('#date').datepicker({dateFormat : 'yy-mm-dd'});,然后通过echo $_POST['date'];查看输出结果,就知道日期是怎么输出的(yyyy-month-date)
猜你喜欢
  • 2014-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多