php - 混合 php 和 javascript - 我们应该在这里使用 json 吗?

标签 php javascript json yii

考虑到以下代码片段:

if('<?= Yii::app()->controller->action->id?>' == 'create'){
 $("#Event_name").focusout(function(){
  $.ajax({
    success: function(html)
    {
     type: 'get',
     url: '<?php echo $this->createUrl('related'); ?>'

这有效。但... 这是正确的做法吗?我希望一侧有 php,另一侧有 javascript,这有意义吗?

有人可以给我举一个例子,说明如果正确完成的话,这样的事情会是什么样子吗?

最佳答案

我对您正在使用的框架没有太多经验,但如果您将变量从 Controller 传递到您的 View ,那就更好了:

Controller :

...
$action_id = Yii::app()->controller->action->id;
$created_url = $this->createUrl('related'); //$this might not be in context here

$this->render('your_view_name', array('action_id'=>$action_id, 'created_url'=>$created_url));
...

查看:

if('<?= $action_id ?>' == 'create'){
 $("#Event_name").focusout(function(){
  $.ajax({
    success: function(html)
    {
     type: 'get',
     url: '<?= $created_url ?>'

关于php - 混合 php 和 javascript - 我们应该在这里使用 json 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11057722/

相关文章:

php - 记录正在搜索的人员和内容?

javascript - jQuery 单击事件不会在 AngularJS DOM 中触发

php - 如何在同一查询中两次列出数据库行?

php - WordPress - CSS 样式不适用于连续

javascript - Ajax 调用后是否可以使用 PHP 重定向页面?

javascript - 试图遍历一个节点得到休息返回

javascript - 带有 Google 日历日期的 Json 字符串

jquery - 通过 jQuery/ajax+JSON 将 ID 和字符串数组传递给 Spring MVC

javascript - 无法将字符串转换为 JSON

php - 在PHP中显示两个日期之间的所有周数