Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

How to get list of selected dates in a text box?

I have a form in which there is "from" and "to" date drop down is present. I want that when i select dates it gets inside a text box.

For example- First i select - From 18 jan 2015 to 20 jan 2015 and then i click on "Add date" and it stores in the input text box just below it. Now i select another range of date and click on "Add date" and it stores inside the same text box after the first range of date.

And in the end i submit my form with these dates.

Please Help...!!!!!!!

My knee-jerk reaction is to recommend jQuery UI DatePicker, but I may not be fully understanding your question. Can you write up a CodePen to show an example of the problem you're having, then post a link here?

Justin Black
Justin Black
24,793 Points

I imagine that's almost correct. While javascript will be mandatory in this. My main question is, from where are you selecting these dates that you wish to display in the textbox?

Javascript for this, is as so:

<script type="text/javascript"> var curVal = $('.textboxwithclass').val(); var newVal = ''; // wherever you are getting the dates from... $('.textboxwithclass').val(curVal + newVal); </script>

Super simple, but will all really depend on where you are selecting these dates from.

Thanks for your replies guys. Let me explain my question in detail. I am making a training management page in which the manager enters the details of the training like training name, venue and dates of training. After entering all these things he clicks on "Create Training" button and all the values are then stored in the database. Now, my single training can be scheduled on different dates example: from 20 oct 2014 to 24 oct 2014 and then again from 26 oct 2014 to 30 oct 2014. So, i need to store these dates in a input text box before i can submit the form i.e. before clicking on the "Create Training" button.

I was thinking of a date-month-year dropdowns. One for "From Date" and other for "To Date". then i click on "Add date" button and the date range is stored in the input text box. Again i select the date range and click on "Add date" button and the other range is stored in the same box but below the value entered before so that final input in the text box become 20 oct 2014 to 24 oct 2014 & 26 oct 2014 to 30 oct 2014.

And in the end i click on "Create Training" button.

Thanks in Advance.

Justin Black
Justin Black
24,793 Points

Based on your description of what you are doing -- what I would do personally, is NOT to have multiple dates for each row.. but to have a startDate and an endDate and give the 'trainer' the ability to duplicate the exact same training giving them the ability to change the dates... UX is more difficult for the trainer, but easier for the end user.

In terms of Ux for the end user, it'll cut down on confusion and is ultimately less code for you to write. IF you must do it this way, then i'd put a single jQuery date range object on the page, and add a button for adding another series of dates... And using javascript, to modify the text box value...

2 Answers

I think I would use the jQuery datepicker to allow the user to select a data, and than use jQuery's .val to get the value of the date picked, which you than could use to do whatever you want.

http://api.jquery.com/val/

You could put that into a disabled input, and than on form submit that input could be saved to a database, sent as an email, whatever you wanted to do.

I'd probably be listening for changes to the selection using jQuery's .on method using the "change" event listener. That way the value will get updated if the user changes the date before the form submits.

http://api.jquery.com/on/

Does jQuery UI Datepicker not meet your needs? You can send both fields up to the database on submit - that's how I'd do it.

I need to send two different ranges of date combined in one text box and then send that to the database. Please let me know how can i do this with datepicker ?

Good luck Bhaskar, I'll defer to a more expert opinion. The problem you're trying to solve is more well defined now, so perhaps someone will chime in with a more appropriate solution.