Jump to content

Date into Form Field with Button?


Recommended Posts

Hi People. 


I am building a logbook application and have a form field where the user will enter a date.


However, I would like to simplify it for the user and give them a button which would add "todays" date into the box for them.


Please could someone tell me the code to do so.  The date format in my DB is YYYY-MM-DD.


The form would also need to take input from the user if they were adding info from a different day instead of today.


Thanks in advance.

<input type="text" name="date" id="date" size = "25"/>
     <input type="submit" name="today" id="today" value="Add Today" />


Link to comment
Share on other sites

I'm still learning PHP so this may not be the best solution.


You could include a php file with the current date as value:


$month = date("m");
$day = date("j");
$year = date("Y");

$date = "$year"."-"."$month"."-"."$day";


This will display 2011-05-16.

Link to comment
Share on other sites

you could also do something where if the user chooses to add todays date..the code will generate a hidden field with todays date, else it will process whatever date the user inserts into the field..is this data going to be entered into a database?

Link to comment
Share on other sites

Look into using a JQuery datepicker. You can configure it to send the value in the correct format. In addition, you can add <select> fields for Y/M/D in the <noscript></noscript> as a backup. Then for either method, just validate the date and insert it into the db.

Link to comment
Share on other sites

If all you need is today's date, you could create a JavaScript function to get the date:


function getTodaysDate() {
var currentTime  = new Date();
var month        = currentTime.getMonth() + 1
var day          = currentTime.getDate()
var year         = currentTime.getFullYear()
var todaysDate   = (year + '-' + month + '-' + day);

document.form.date.value = todaysDate;



Then just call the function using an anchor tag near the form field:


<form name="form" action="#">

<input type="text" name="date" id="date" size = "25"/></label> <script type="text/javascript">document.write('<a href="#dateField" name="dateField" onclick="getTodaysDate();">Use Today\'s Date</a>');</script>




Note that if you wanted to use a solution like this, you'll need to look into padding the dates.


Link to comment
Share on other sites

You make sure you validate the value as a valid date. If you're going to use input fields for a date, you should not use type="text" for a date, since you have no way to know what the user's intention is when they enter a value such as 09/08/03. Is it Sep 8 2003? Is it 9 Aug 03? is it Aug 3, 09?


You have much better control over the process if you use a datepicker, with <select> fields as a <noscript> backup.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.