Jump to content

Date into Form Field with Button?


VinceGledhill

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"/>
    </label>
     <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:

<?php

$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() {
//GET TODAY'S DATE
var currentTime  = new Date();
var month        = currentTime.getMonth() + 1
var day          = currentTime.getDate()
var year         = currentTime.getFullYear()
var todaysDate   = (year + '-' + month + '-' + day);

//POPULATE THE FORM FIELD
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>

</form>

 

 

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

http://www.electrictoolbox.com/pad-number-zeroes-javascript/

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 thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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