Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Input 2

Status
Not open for further replies.

andy98

Programmer
Jul 7, 2000
120
GB
Hi,
Hopefully, this will be an easy one for someone!

I have a form with an Order date field and I want to have the user to be able to enter the date in British date format (dd-mm-yyyy).

Any ideas how I would accomplish this?

 
dropdown list?
Code:
<select name="day">
<?php
$selected = "";

if (!isset($day)) {
  echo "<option value="">DD</option>";
  }
for ($i = 1; $i < 31; $i++) {
    if (isset($day)) {
      if ($day == $i) {
        $selected = " selected=\"selected\" ";
        }
      }
    echo "<option value="{$i}"{$selected}>{$i}</option>";
  }
</select>

then you do the same for month and year.

Olav Alexander Mjelde
Admin & Webmaster
 
Many thanks!

And how would I put that all back togather for insertion into the database?
 
Hi

I want to be able to insert the values for 'Day' 'Month' 'Year' from that select box (drop-down list)on my form into a string to insert into my mySQL DB which accepts a DATE field type on my update page.

I was hoping something like the following would work, but it doesn't - any ideas?

Code:
$order_date = $_POST['year']$_POST['month']$_POST['day']
 
When you "glue" together strings, you use .
$order_date = $_POST['year'] . $_POST['month'] . $_POST['day'];

You can also glue together, if you put the variables inside "":

$foo = "{$_POST['year']}{$_POST['month']}{$_POST['day']}";

I dont know which is faster, but I've been told (a long time ago) that using . to glue strings is slow.

I havent tested those two ways against each-other.

ps. I guess you have to convert the date to proper syntax. eg. if you wish to update TIMESTAMP, you might want to add 01 instead of 1 (for day 1).

Olav Alexander Mjelde
Admin & Webmaster
 
SELECT DATE_FORMAT('2005-12-1', '%Y%m%d');

outputs: 20051201

So, you'll do something like:
"INSERT INTO foo SET bar=DATE_FORMAT('2005-12-1', '%Y%m%d') WHERE id=...";

only, you'll insert a variable, ofcourse..
Also, remember to do some checking of variables, before they touch your database.

ref: Also, use trim() and strip_tags() on the varibles.

I would make an own function that can clean variables, before they are used for further processing.

The first thing you do, is trim(), to remove whitespace.
Then, you remove unwanted tags, with strip_tags().

After this is done, you can use the real_escape_string.
Please read up on the example 3A


You can take that function and modify it to your needs.

remember that you can wrap functions like:
Code:
$value = stripslashes(strip_tags(trim($value)));


Olav Alexander Mjelde
Admin & Webmaster
 
Hopefully getting there:

Code:
$query="UPDATE acc_order 
           SET order_date          = '$order_date', 
	       order_dispatch_by   = '$order_dispatch_by',
               airbill_id          = '$acc_airbill_id'
	       
 WHERE order_id = '$_order_id'";

How would I do the dateformat insert for ORDER_DATE in my above query?
 
Code:
$order_date = stripslashes(strip_tags(trim("{$yyyy}-{$mm}-{$dd}"))); // read what I said above about securing variables.

$query="UPDATE acc_order 
           SET order_date      = 'DATE_FORMAT($order_date, '%Y%m%d')', 
             order_dispatch_by = '$order_dispatch_by',
             airbill_id        = '$acc_airbill_id'
             WHERE order_id    = '$_order_id'";

ps. code not tested, so it might not work "out of the box".

Olav Alexander Mjelde
Admin & Webmaster
 
Don't know why: but have implemented your suggestions but am not getting the date returned. It just comes back as 00-00-0000.

This is what I have for my drop down date selection box

Code:
<select name="day"> 
<?php 
for ($d = 1; $d <= 31; $d++) 
echo "<option value=\"" . $d . "\">" . $d . "</option>"; 
?> 
</select> 
<select name="month"> 
<?php 
for ($m = 1; $m <= 12; $m++) 
echo "<option value=\"" . $m . "\">" . $m . "</option>"; 
?> 
</select> 
<select name="year"> 
<?php 
$max_year = 2005;
for ($y = 2004; $y <= $max_year; $y++) 
echo "<option value=\"" . $y . "\">" . $y . "</option>"; 
?> 
</select>

then this is what I have on my form action page:

Code:
$acc_order_dispatch_date = $_POST['year'] . $_POST['month'] . $_POST['day'];

$order_id                 = $_GET[order_id'];

$acc_order_dispatch_date = stripslashes(strip_tags(trim("{$yyyy}-{$mm}-{$dd}"))); 

$query="UPDATE acc_order 
  SET acc_order_dispatch_date  = 'DATE_FORMAT($acc_order_dispatch_date, %Y%m%d)', 

WHERE order_id = '$order_id'";
	   
$result = mysql_query($query) or die (mysql_error());

and then on my display page I use the following:
Code:
$query2 = "SELECT  *, DATE_FORMAT(acc_order_dispatch_date, '%d-%m-%Y') AS niceDate2

To display it in a British format.

Can you see where I am going wrong?

Many thanks

Andy
 
Problem 1: How are you sending the data to the next page? Is it method post or get? If first, your $_POST[] is correct, if second, you should use $_GET[].
Problem 2: Where are $yyyy, $mm and $dd coming from? You should try:
Code:
$acc_order_dispatch_date = stripslashes(strip_tags(trim("{$_POST['year']}-{$_POST['month']}-{$_POST['day']}")));
Hope it helps.
 
Code:
$acc_order_dispatch_date = stripslashes(strip_tags(trim("{$_POST['year']}-{$_POST['month']}-{$_POST['day']}")));

That's done the trick! Many thanks!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top