0

I am trying to get a jquery array into a html input and I can't figure out where I am wrong.

HTML:

<input type="hidden" id="datepicker" name="dates[]" />

Jquery:

<script>
$('#datepicker').datepick({
    dateFormat: 'yyyy-mm-dd',
    multiSelect: '100'
});
$(document).ready(function() {
    $("#butto").click(function() {
        var dates = $('#datepicker').datepick('getDate');
        console.log(dates);

        $('#datepicker').val(dates);
    });
});
</script>

PHP:

$dates= $this->input->post('dates');

foreach($dates as $datee) {
    print_r($datee);
}
6
  • Are you getting value to this ` var dates ` variable ? Commented Oct 9, 2018 at 7:15
  • Which datepicker library are you using? Commented Oct 9, 2018 at 7:16
  • If you use a lib the question is not "How to get jquery array in html input" but "How can I use %LIB_NAME% to add data in my input". Tell us the lib name or check the documentation :) Commented Oct 9, 2018 at 7:19
  • @SilentCoder no i am not getting any value in print_r($datee);. That is the problem Commented Oct 9, 2018 at 7:21
  • @RoryMcCrossan keith-wood.name/datepickRef.html#getDate this one. but i am getting array in console. Commented Oct 9, 2018 at 7:22

3 Answers 3

1

You can't directly get the dates array, because the multi select dates are passing by comma(,) separated by default. So you need to do small hack before submitting the form data. Bellow is the self defined code to achieve the result.

jQuery Code

<script>
    $('#datepicker').datepick({

      dateFormat: 'yyyy-mm-dd',
      multiSelect: '100',

      //convert the selected date into array and assign the value to your hidden filed when the date picker will close by any format

      onClose: function(dates) { 
         var selectedDate = []; 
         for (var i = 0; i < dates.length; i++) { 
             selectedDate.push($.datepick.formatDate(dates[i])); 
         } 
         $('#datepicker').val(JSON.stringify(selectedDate));
      },

   });
</script>

PHP Code

In the server side catch the hidden filed value and decode it using json_decode function to get the array of dates.

$dates= json_decode($this->input->post('dates'));

foreach($dates as $datee) {
   print_r($datee);
}

Hope it will work as your expected.

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

Comments

0

try this,

 // if $dates = ['2018-10-9','2018-10-10']
$dates= $this->input->post('dates');
if(count($dates)!=0){
  $isDate = "";
  for($i=0; $i<=count($dates); $i++) {
    $isDate .= $dates[$i].',';
  }
  $isDate = rtrim($isDate,',');
  print_r($isDate);
  // will return $isDate = "'2018-10-9','2018-10-10'";
} else {
  print_r("no dates");
}

Comments

0

Got the Answer. HTML:

*<input type="hidden" id="datepicker1" name="dates[]" />*

Jquery:

<script>
$('#datepicker').datepick({
    dateFormat: 'yyyy-mm-dd',
    multiSelect: '100'
});
$(document).ready(function() {
    $("#butto").click(function() {
        var dates = $('#datepicker').datepick('getDate');
        console.log(dates);

        *$('#datepicker1').val(dates);*
    });
});
</script>

Comments

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.