du1843 2014-01-25 20:30
浏览 72
已采纳

在JQuery中插入PHP

I am trying to embed a data picker calendar in my php site. The code, which came off the shelf from DatePicker, allows to select some pre-highlighted dates, which is good as I have a list in a db table with the dates I need.

However, I have had no success in finding a way to include php within the script, or the other way round.

The original calendar script is as follows:

<script type='text/javascript'>

$(function() {
$("#txtDate").datepicker({dateFormat: 'yy-mm-dd'});
});

$(document).ready(function() {

    var SelectedDates = {};
    SelectedDates[new Date('02/24/2014')] = new Date('02/24/2014');
 SelectedDates[new Date('03/10/2014')] = new Date('03/10/2014');

    $('#txtDate').datepicker({
        beforeShowDay: function(date) {
            var Highlight = SelectedDates[date];
            if (Highlight) {
            return [true, "Highlighted", Highlight];
        }
        else {
            return [true, '', ''];
        }
    }
});
});
  </script>

I would like to be able to select a long list of dates to go in SelectedDates[new Date('03/10/2014')] = new Date('03/10/2014');

so my original idea was to do as follows:

$(document).ready(function() {
        var SelectedDates = {};

  <?php 
$query = "SELECT eventDate FROM database.calendar WHERE tag='R' AND competition='1'";
$result = mysql_query($query);
while ($row = mysql_fetch_row($result)) {
    $eventdate = $row[0];

SelectedDates[new Date('$eventdate')] = new Date('$eventdate');
}
?>

Sadly, this doesn't work (and neither do any of the various attempts to re-add tags within the PHP.

Any idea? Thank you so much for your help!

  • 写回答

1条回答 默认 最新

  • dougang6178 2014-01-25 20:34
    关注

    You have to close your PHP tags to output JavaScript (or use echo as mentioned in a comment):

    $(document).ready(function() {
        var SelectedDates = {};
    
        <?php 
        $query = "SELECT eventDate FROM database.calendar WHERE tag='R' AND competition='1'";
        $result = mysql_query($query);
        while ($row = mysql_fetch_row($result)) {
            $eventdate = $row[0];
            ?>
            SelectedDates[new Date('<?php echo $eventdate; ?>')] = new Date('<?php echo $eventdate; ?>');
            <?php
        }
        ?>
    });
    

    Although I should mention that what you are trying to do is not really ideal. Do the PHP while loop separately, get that into an array, and json_encode that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?