2

Iam using This multiple date picker. I want to add dates dynamically into this text box.But it is not showing inside the text box. Jsfiddle added.

<input type="text" id="from-input" value="02/20/2014">

My php code is like below

<?php
    while($fetch=mysql_fetch_array($qry))
    {
?>
        <input type="text" id="from-input" value="<?php echo $fetch['sel_date']; ?>">
<?php
    }
?>

http://jsfiddle.net/bJ7zj/#update

enter image description here

4
  • 1
    It's working for me with Chrome Commented Feb 24, 2014 at 6:43
  • 1
    No value="02/20/2014" not showing by default. Commented Feb 24, 2014 at 6:46
  • I thought you were talking about after you clicked on the dates they weren't populating the input. Perhaps you should make your question a bit more clear. Commented Feb 24, 2014 at 6:49
  • My problem is when i set value of textbox with 02/20/2014, it is not showing on the textbox while executing the web page. Commented Feb 24, 2014 at 6:52

1 Answer 1

2

You have a couple of problems.

First, you can't just add a value to the element you add multiDatesPicker to and expect that date to be selected because it will clear the values. Instead, use an alternate field, like this:

<div class="box">
<input type="text" id="from-input">
</div>
<input type="hidden" id="orig-dates" value="<?php echo $fetch['sel_date']; ?>">

Then in your JS, you first create the datepicker, then add the dates to it:

$('#from-input').multiDatesPicker();
var orig_dates_str = $("#orig-dates").val();
    if (orig_dates_str.length > 0) {
        var orig_dates_arr = orig_dates_str.split(",");
        var dates = new Array();
        for (i=0; i<orig_dates_arr.length; i++) {
            numbers = orig_dates_arr[i].split("-");
            d = new Date(numbers[0], numbers[1] - 1, numbers[2]);

            dates.push(d);
        }
        $('#from-input').multiDatesPicker('addDates', dates);
    }

Then your second error is that the format "MM/DD/YYYY" is not recognized. Instead, you need to do "YYYY-MM-DD".

In PHP you can do this like so:

$date = new DateTime($fetch['sel_date']);
echo $date->format('Y-m-d'); // Put this into the <input>

See it on JSFiddle

Sign up to request clarification or add additional context in comments.

2 Comments

I also want to show 2014-02-20 inside the from-input text box. Thanks @Mike
You have to put it there then. $('#from-input').val(orig_dates_str);. [Fiddle]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.