dongxun2089
2017-09-11 21:46
浏览 58

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

    点赞 打赏 评论

相关推荐 更多相似问题