通过ajax提交选择框的值,并且显示结果不起作用

时间:2019-06-05 10:25:29

标签: javascript php jquery ajax

我有一个表格,该表格具有一年中月份的选择框,选择任何月份后,都应在同一页面上显示该月中的所有日期。我试图使用ajax来实现这一点,但是以某种方式我没有得到任何结果。当我直接查看通过ajax调用的页面时,在那里得到了预期的结果。无法弄清楚为什么主页不显示结果。

主页

 <form method="post" action="" name="form_cal" id="form_cal">
    <div>
        <span><label class="h5"><strong>Month for Screening</strong></label></span>
        <span>                          
            <select name="month" id="month" value="" tabindex="1" aria-hidden="true" required style="height:50px;display: block;padding:10px;" class="h5">
                <option value="" selected="selected">- Select One -</option>
                <option value="1">January</option>
                <option value="2">Febuary</option>
                <option value="3">March</option>
                <option value="4">April</option>
                <option value="5">May</option>
                <option value="6">June</option>
                <option value="7">July</option>
                <option value="8">August</option>
                <option value="9">September</option>
                <option value="10">October</option>
                <option value="11">November</option>
                <option value="12">December</option>
            </select>
        </span>
    </div>
</form>
<div id="calendar-display"></div>

JS

<script type='text/javascript'>
$(document).ready(function(){ 
  var timer = null; 
  var dataString; 
  function submitForm(){
      $.ajax({ type: "POST",
                url: "calendar-action.php",
                data: dataString,
                success: function(result){
                    $('#calendar-display').php(result);
                }
      });
      return false;
  }
  $('#month').on('change', function() {
     clearTimeout(timer);
     var month = $(this).val();
     dataString = 'month='+ month;
     timer = setTimeout(submitForm, 050);
 });
});

然后在PHP中由ajax调用的页面

<?php 
if($_POST){

        date_default_timezone_set('Africa/Lagos');
        $date = date('Y-m-d H:i:s');
        $ucimslots = "25";
        $curyear = date("Y");
        $month = mysqli_real_escape_string($connQlife, $_POST['month']);
        $day = date('j');
        $daysinmonth = date('t',mktime(0,0,0,$month,1,$curyear));
        //$daysleftinmonth = $daysinmonth - $day;
}
?>

        <div id="calendar-display">
        <?Php for($day = 1; $day <= $daysinmonth; $day++){ ?>
        <div class="calendarcont">
            <div class="calendarheadercont">
                <div class="calendarday"><?Php echo date("l", mktime(0, 0, 0,$month,$day,$curyear)); ?></div>
                </div>
                <div class="clear_1"></div>
                <div class="calendarsubcont">
                    <div class="calendardatecont">
                        <div class="calendarmonth"><?Php echo date("M", mktime(0,0,0, $month,$day,$curyear)); ?></div>
                        <div class="calendardate"><?Php echo date("j", mktime(0,0,0,$month,$day,$curyear)); ?></div>
                 </div>
                 <div class="calendartextcont">
                     <div class="calendartext">Available slots: <?Php echo $availableslots; ?></div>
                 </div>
             </div>                                         
          </div>
        <?Php } ?>                   
        </div>

1 个答案:

答案 0 :(得分:1)

在JavaScript中,您尝试进行php()函数调用,该调用无效:

$('#calendar-display').php(result);

相反,您应该使用html()

$('#calendar-display').html(result);