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.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!