Jump to content

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
https://forums.phpfreaks.com/topic/236492-date-into-form-field-with-button/
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.

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/

Actually, now that I think about the original question why have the visitor enter today's date at all? Couldn't you just add the date after the form is submitted?

 

You could use the date() function (http://php.net/manual/en/function.date.php) in the script that processes the form:

 

$todaysDate = date("Y-m-d");

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.

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.