2014-02-24 34 views
0

我想连接json对象和cakephp代码之间。形成有效的超链接。concat JavaScript与cakephp

这是JSON对象= item.Customer.customers_name

我想JSON对象(客户名称)是在超链接的CakePHP显示。

的最终结果将是

<li class="icn_list_users"> 
<a href="/admin/scores/edit_test_a_1/345/abcdef/2949/scores">Ty John</a> 
</li> 

不是我似乎能顶CONCAT js和PHP的吻合。

<script type="text/javascript"> 

    $.getJSON("http://localhost:8888/tests/teststudents.json?id=<?php echo $this->params['pass'][1]; ?>", function(data) { 

     var ul = $('#teststudents'); 

     $.each(data, function (i, item) { 
     ul.append($('<li class="icn_list_users"><?php echo $html->link(' + item.Customer.customers_name + ', array('admin'=> true, 'controller' => 'scores','action' => $scoresheetpath, $this->params['pass'][1],$range)); ?></li>')); 
     }); 

     }); 

    </script> 

回答

2

您不能将PHP和javascript结合起来。 PHP是服务器端语言,Javascript是客户端。这意味着PHP将首先被解析,发送到客户端,然后Javascript将被解析。如果你需要在Javascript中建立链接,用jQuery来做。你可以用PHP设置Javascript变量,但不能用其他方式。

也请看到这个问题:What is the difference between client-side and server-side programming?

0

如鹰说,你不能因为这个概念是不同的Concat的PHP和JSON对象。但是你可以做的是对代码进行一些调整,你可以根据需要显示你的列表。

<script type="text/javascript"> 

    $.getJSON("http://localhost:8888/tests/teststudents.json?id=<?php echo $this->params['pass'][1]; ?>", function(data) { 

     var ul = $('#teststudents'); 

     $.each(data, function (i, item) { 
     ul.append('<li class="icn_list_users"><a href="/admin/scores/edit_test_a_1/345/abcdef/2949/scores">' + item.Customer.customers_name + '</a></li>'); 
     }); 

     }); 

</script> 

希望它有帮助。