0

I am using SQL to query some dates (datetime Y-m-d H:i:s) from a database and putting them into a select box. The problem is when I am using $_POST to send the selected value to the next page, only the date appears, the time has gone missing. Please can you help, thanks.

eg. 2015-02-11 12:34 changes to just 2015-02-11

From the first page:

            session_start()
            ####some codes here#### 
            echo '<form action ="selected.php" method    ="POST">';
            echo "<select name='production_time' class='input'></option>"; 
            foreach($res as $row) {
            $mysqldate = date('Y-m-d H:i:s', strtotime($row['date_time']) );
            echo "<option value=$mysqldate>$mysqldate</option>"; 
                                }
            echo "</select>"; 
            echo "<p> </p> <br/ > ";
            echo '<p><input type="submit" class ="button"></p>';
            echo '</form>';

From the second page:

                                session_start();
                                echo '<div id="middle">';
                                $selected_date = $_POST['production_time'];
                                echo $selected_date ;
                                echo '<br />';
                                echo '</div>';
cooldev
  • 13
  • 1

1 Answers1

0

Try adding quotes...

  echo "<option value=\"{$mysqldate}\">{$mysqldate}</option>";

Look at the page source to compare the output with and with out quotes. I think it should look rather obvious. The values need to be encapsulated somehow.

More details on select/option in HTML:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select

For more on what can go in the value of a select option see this good answer:

Can the select option value be of different types?

Community
  • 1
  • 1
ficuscr
  • 6,975
  • 2
  • 32
  • 52