2017-09-11 21:46
浏览 60

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";
        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">
    <select name="distributor_id">
      <?php foreach ($distributors as $distributor) : ?>
        <option value="<?php echo $distributor['distributorID']; ?>">
            <?php echo $distributor['distributorName']; ?>
      <?php endforeach; ?>
    <label>Order Code:</label>
    <input type="text" name="code">
    <label>Customer Name:</label>
    <input type="text" name="name">
    <label>Order Date:</label>
    <input type="text" id="date" name="date">
    <input type="submit" value="Add order">
  <p class="last_paragraph">
    <a href="index.php?action=list_orders">View order List</a>

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-->
  <!-- This is the body section-->
    <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="//">
          <link rel="stylesheet" href="/resources/demos/style.css">
          <script src=""></script>
          <script src=""></script>

          $( function(){
            $( "#date" ).datepicker();
          } );

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

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


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)
                (: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);

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>
      <th>Order Code</th>
      <th>Order Name</th>
      <th>Order Date</th>
    <?php foreach ($orders as $order) : ?>
        <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">
    <?php endforeach; ?>
  <p class = "last_paragraph">
    <a href="?action=show_add_form">Add order</a>

    ***<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条回答 默认 最新

相关推荐 更多相似问题