2014-11-04 30 views
-2

我無法弄清楚如何在下面的Javascript中運行這個PHP?如何運行PHP內部的Javascript

這是我的PHP:

<?php query_posts('post_type=calendar'); 
     if (have_posts()) : while (have_posts()) : the_post(); ?> 
{  
title: '<?php the_field('program_title'); ?>', 
url: '<?php the_field('link'); ?>', 
start: '<?php the_field('start_date'); ?>', 
end: '<?php the_field('end_date'); ?>' 
}, 
<?php endwhile; endif; ?> 

這裏是我的javascript:

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#calendar').fullCalendar({ 
      header: { 
       left: 'prev,next today', 
       center: 'title', 
       right: 'month,basicWeek,basicDay' 
      }, 
      editable: false, 
      eventLimit: true, // allow "more" link when too many events 
      events: [ 

PHP GOES HERE 

      ] 
     }); 

    }); 
</script> 
+0

您可能想要做的就是使用AJAX將.php文件的html輸出加載到DOM元素中。 如果我誤解了你正在尋找的東西,也許你需要讓php生成javascript,因爲在DOM提供給用戶之前,php會執行並更改腳本。 – Ambiguities 2014-11-04 19:06:40

+0

不可能。由於PHP將運行在服務器和JavaScript上,正常運行在客戶端上,您將需要執行一些AJAX。在呈現頁面之前或之後的 – 2014-11-04 19:07:03

+0

? – Daredzik 2014-11-04 19:10:24

回答

-3

因爲我不能評論

我假設the_field()的返回某種價值。函數輸出(回顯/打印/等)的值?如果沒有,請嘗試輸出它

{  
title: '<?php echo the_field('program_title'); ?>', 
url: '<?php echo the_field('link'); ?>', 
start: '<?php echo the_field('start_date'); ?>', 
end: '<?php echo the_field('end_date'); ?>' 
}, 

此外,你可能想查找php的函數json_encode()。在這種情況下,這可能對你有用。

+0

我在json http://fullcalendar.io/docs/event_data/events_json_feed/上找到了這個頁面,並將我的php放到一個名爲calendar.php的文件中,但仍然沒有運氣 – johnnyr209 2014-11-04 20:16:56