dongxun2089 2017-09-11 21:46
浏览 61
已采纳

PHP表单与jQuery DatePicker

I am building a MVC PHP web app with MySQL. I am using a form to collect information from the user. The date input works when date information is input in the correct order but I want this to be more user friendly for my less tech savvy coworkers (God Bless'Em).

Here is my form in '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");
      }
}
?>

Here is my form in '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>

Here is the view '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>

Here is my Model '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();
}
?>

The jQuery DatePicker displays and even populates the form field, but the table in my DB shows "0000-00-00".

My question is this: how do I take the DatePicker data, and convert it into a Date format that MySql will store?

Thank for taking your time to look at my question.

<------ This is my first time posting so if I'm doing it wrong... whoops!----->

This is a part of my 'order_list.php' file

<!-- 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.

  • 写回答

1条回答 默认 最新

  • dsj2014 2017-09-11 21:59
    关注

    Hello Buhlahkay.

    • Make sure your are using your Database structure for Date is Using DATE format.. Date field for mysql are accepted 2017-12-30 (Year-Month-Date).

    • Debug your self the code output the echo $_POST['date']; and look at the date whether its formatting Database accepted format (Year-Month-Date).

    • Modify the Date Picker Jquery as $('#date').datepicker({dateFormat : 'yy-mm-dd'});

    • If it displays the Format there is no issue.. Kindly Re update the codes properly as the what does index.php is doing like some more detailed 1 by 1. to avoid confusion..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿