VinceGledhill Posted May 15, 2011 Share Posted May 15, 2011 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" /> Quote Link to comment https://forums.phpfreaks.com/topic/236492-date-into-form-field-with-button/ Share on other sites More sharing options...
r0b Posted May 15, 2011 Share Posted May 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236492-date-into-form-field-with-button/#findComment-1215814 Share on other sites More sharing options...
fugix Posted May 16, 2011 Share Posted May 16, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/236492-date-into-form-field-with-button/#findComment-1215840 Share on other sites More sharing options...
Pikachu2000 Posted May 16, 2011 Share Posted May 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236492-date-into-form-field-with-button/#findComment-1215886 Share on other sites More sharing options...
cyberRobot Posted May 16, 2011 Share Posted May 16, 2011 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/ Quote Link to comment https://forums.phpfreaks.com/topic/236492-date-into-form-field-with-button/#findComment-1215986 Share on other sites More sharing options...
cyberRobot Posted May 16, 2011 Share Posted May 16, 2011 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"); Quote Link to comment https://forums.phpfreaks.com/topic/236492-date-into-form-field-with-button/#findComment-1215990 Share on other sites More sharing options...
VinceGledhill Posted May 16, 2011 Author Share Posted May 16, 2011 Brilliant, thanks for that, I have declared the $today variable and have echo'd it into the box. That will be there by default, if the user wants a different "date" then they can enter it. Sorted. Quote Link to comment https://forums.phpfreaks.com/topic/236492-date-into-form-field-with-button/#findComment-1216082 Share on other sites More sharing options...
Pikachu2000 Posted May 16, 2011 Share Posted May 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236492-date-into-form-field-with-button/#findComment-1216087 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.