在我的 cakephp 應用程序中,我試圖創建一個評論功能。所以我使用 ajax 請求來獲取評論,然后克隆一個 html 模板并將其附加到頁面://load comments $(document).ready(function(){ $.ajax({ type: 'POST', url: '/app/tasks/getComments', datatype: 'json', beforeSend: function (xhr) { // Add this line xhr.setRequestHeader('X-CSRF-Token', $('[name="_csrfToken"]').val()); }, data: { task_id : "<?php echo $task->id; ?>" }, success: function( data ) { for (var i=0; i < data.length; i++) { //Clone the template var item = $(template).clone(); document.cookie = 'photos['+i+']='+data[i].user.photo; document.cookie = 'names['+i+']='+data[i].user.username; //Find the element $(item).find('#comment_photo').html('<?php if(isset($_COOKIE["photos[$num]"])) {echo $this->Html->image($_COOKIE["photos[$num]"], ["class" => "avatar avatar-online", "alt" => $currentUser->username]); } ?>'); $(item).find('#comment_username').html('<?php if(isset($_COOKIE["names[$num]"])) { echo $_COOKIE["names[$num]"]; }?>'); $(item).find('#comment_time').html(moment(data[i].created_date, 'DD-MM-YYYY hh:mm:ss').format('MMMM Do YYYY, h:mm:ss a')); $(item).find('#comment_text').html(data[i].comment); $(item).find('#comment_id').html(data[i].id); //Append to the source $('#target').append(item); } } }); });為了在 php 中訪問返回的結果,我將它們存儲在 cookie 數組 ( photos[], names[]) 中。我如何設置和更新 的值$num以便在每個 html 代碼附加中迭代數組?用這種方法可以實現我想要的嗎?或者我需要一個全新的?
- 1 回答
- 0 關注
- 267 瀏覽
添加回答
舉報
0/150
提交
取消
