Search code examples
jqueryajaxdatepicker

jQuery datepicker not working after call Ajax


jQuery datepicker does not work after calling Ajax

I'm using jQuery datepicker to get posts from database(datewise). I tried this code `

<script type="text/javascript"> 
     $(function() { $("#datepicker").datepicker({ dateFormat: "yy-mm-dd" }).val() }); 
     </script> 
     Date: <input type="text" id="datepicker" size="30"/>

` It's working fine.

Now I call ajax like this, it's not showing calender.not working also.Is it correct? I need your help `

$(function() {
     var currentTime = new Date();
     var day = currentTime.getDate();
     var month = currentTime.getMonth() + 1;
     var year = currentTime.getFullYear();

     if (day < 10){
     day = "0" + day;
     }

     if (month < 10){
     month = "0" + month;
     }

     var today_date = day + "/" + month + "/" + year;
     var dataString ='date='+today_date;

     $("#datepicker").datepicker({

     onSelect: function(dateTypeVar, instance) {
     var dateAsObject =   $("#datepicker").datepicker({dateFormat: "yy-mm-dd"}).val();
     var dataString ='date='+dateAsObject; 
     $.ajax
     ({
              type: "Post",
              url: "<?php echo home_url(); ?>/?datepost",
              data: dataString
              success: function(data)
              {
                  $('#testdiv').html(data);
              }
         });  
   
     }
     });

     });
     </script>

`

This is Mysql Query in function.php
`

if(isset($_REQUEST['datepost']))
     {
     $date = $_POST['date'];
     $res=mysql_query("SELECT ID FROM  `wp_posts` WHERE DATE_FORMAT(post_date,  '%m/%d/%Y') =  '".$date."' AND post_type = 'post'  ORDER BY post_date DESC
LIMIT 0 , 1");
     $post_id=mysql_fetch_array($res);
     $pid = $post_id['ID'];
     exit();
     }`

Solution

  • You haven't added comma after this line data: dataString in the ajax request, which is might be breaking your js code.

    Try adding a comma.

    $.ajax ({
        type: "Post",
        url: "<?php echo home_url(); ?>/?datepost",
        data: dataString,
        success: function(data) {
            $('#testdiv').html(data);
        }
    });