2015-10-15 103 views
0

我正在使用fullcalendar.js,并且日历正在运行,因为它现在应该是。如果用户点击日历中的条目,则模式会显示一些信息。我现在需要的是,模式中的信息是来自数据库的信息,所以我想插入一些PHP代码,但我不知道如何。将PHP代码转换为Javascript

让我告诉你,我想用这样从我的数据库中的数据在模式显示的PHP代码:

<?php 
    $dates=getPcalInfoOfHour($gl_userid,0,0); 
     for ($x=0;$x<count($dates["id"]);$x++) { 
      Title: '".$dates["title"][$x]."'; 
      Selected Date: '".$dates["date"][$x]."'; 
      Selected Time: '".$dates["start"][$x]."' '".$dates["end"][$x]."'; 
      Topic: '".$dates["topic"][$x]."'; 
      Location: '".$dates["location"][$x]."'; 
      Address: '".$dates["address"][$x]."'; 
     } 
?> 

这里就是我想进入我的JavaScript代码的一部分我的PHP代码:

eventClick: function(calEvent, jsEvent, view) { 

     //display a modal 
     var modal = 
     '<div class="modal fade">\ 
      <div class="modal-dialog">\ 
      <div class="modal-content">\ 
      <div class="modal-body">\ 
       <button type="button" class="close" data-dismiss="modal" style="margin-top:-10px;">&times;</button>\ 
       <label> <strong>Topic:</strong> Topic</label>\ 
       <label> <strong>Selected Dated:</strong> Date</label>\ 
       <label> <strong>Selected Time:</strong> Time</label>\ 
       <label> <strong>Title:</strong> Title</label>\ 
       <label> <strong>Location:</strong> Location</label>\ 
       <label> <strong>Address:</strong> Address</label>\ 
       <label> <strong>Phone:</strong> Phone</label>\ 
      </div>\ 
      <div class="modal-footer">\ 
       <button type="button" class="btn btn-sm" data-dismiss="modal"><i class="ace-icon fa fa-times"></i> Close Window</button>\ 
      </div>\ 
      </div>\ 
     </div>\ 
     </div>'; 

     var modal = $(modal).appendTo('body'); 

     modal.modal('show').on('hidden', function(){ 
      modal.remove(); 
     }); 

     console.log(calEvent.id); 
     console.log(jsEvent); 
     console.log(view); 

    } 

我知道PHP是一个服务器端语言和JavaScript是基于客户端的语言,但必须有一个办法,从数据库中我的数据会模态内可以显示我是正确的?我对PHP编程有一点了解,但我从来没有用JavaScript写过东西。

如果有人能告诉我我的代码应该是什么样子,以便从模式窗口中的数据库中获取我的信息,我将非常感激。

谢谢, 克里斯

+0

你可以写像'变种PHP = '和'alert(php)'我认为它的工作 –

+0

如果数据被用户填满,您很容易受到XSS攻击。 – GuyT

+0

[JavaScript中的访问PHP变量](http:// stackoverflow。com/questions/4287357/access-php-variable-in-javascript) – moonwave99

回答

0

我相信这会做:

var js_string = "<?php echo $php_string; ?>"; 
0

你必须要请求服务器PHP脚本究竟会从数据库提取数据并生成HTML。

例JavaScript和PHP的使用生成HTML模式:

$.post('/script_generating_html.php', params, function(response) { 
    var modal = $(response).appendTo('body'); 

    modal.modal('show').on('hidden', function(){ 
     modal.remove(); 
    }); 
}); 
0

认为它这样。 PHP在发送任何内容之前会生成浏览器将读取的内容所以,如果你有以下代码

<?php 
 
    $data = "Super Important"; 
 
?> 
 

 
<script> 
 
    alert("<?php print $data; ?>"); 
 
</script>

它将为客户端生成

<script> 
 
    alert("Super Important"); 
 
</script>

如果你要与修改Javascript工作用php,也看看json_encode()

的另一种方式acheive同样的事情是

<?php 
 
    $data = "Super Important"; 
 
?> 
 

 
<script> 
 
    alert(<?php print json_encode($data;) ?>); 
 
</script>