doutao5499 2017-04-03 15:00
浏览 83
已采纳

使用数据库中的结果作为jquery中的变量

My jquery datepicker has the following code:

$(document).ready(function() { 

    var event = ['14 april 2017', '25 april 2017', '1 april 2017'];

$('#datepicker').datepicker({
      inline: true,
      showOtherMonths: true,
      dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
      beforeShowDay: highlightDays,
      onSelect: function(dateText, inst){
        showpopup();
        getdatespan();
            }
    });


function highlightDays(date) {
      for (var i = 0; i < event.length; i++) {
            if (new Date(event[i]).toString() == date.toString()) {
                return [true, 'ui-state-event', ''];
            }
        }
        return [true, ''];
    }
...

The bit I'd like to focus on is the variable 'event' that is being used to provide dates for the highlightDays function. As it is above, the code works perfectly and successfully highlights the date in the variable. However, those dates have been manually typed in by myself.

Below is the code within my calendar.php file:

$findEventStartdates = $pdo->prepare('SELECT startdate FROM calendar WHERE username=?'); 

//execute query with variables
$findEventStartdates->execute([$username]);


($eventStartdates = $findEventStartdates->fetchAll(PDO::FETCH_COLUMN));

When printed, this shows the following output:

Array ( [0] => 14 april 2017 [1] => 25 april 2017 [2] => 1 april 2017)

My question is how can i get whatever is outputted in that PDO to be used as the variable for the datepicker?

In the PHP file i tried the following:

$highlightEvents = implode("', '",$eventStartdates);

echo <<<_END
 <input type="text" name="eventHighlight" id="eventHighlight" value="'$highlightEvents'">
_END; 

This shows:

'14 april 2017', '25 april 2017', '1 april 2017'

in the textbox and then tried this with the jquery:

var event = [$("#eventHighlight").text];

but this did not work. So any alteration to this or different way of using the Array would be appreciated.

Thank you.

  • 写回答

2条回答 默认 最新

  • dqysi86208 2017-04-03 15:41
    关注

    You can use json_encode() on php side to print the array directly into a variable in javascript:

    var event = <?php echo json_encode($eventStartdates);?>;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器