Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CodeIgniter:如何将javascript变量发送到contoller并返回到视图?_Javascript_Php_Codeigniter - Fatal编程技术网

CodeIgniter:如何将javascript变量发送到contoller并返回到视图?

CodeIgniter:如何将javascript变量发送到contoller并返回到视图?,javascript,php,codeigniter,Javascript,Php,Codeigniter,我正在尝试在我的查看器中实现变量的实时更新,这几乎完成了。问题是我现在想比较变量的结果,这样我就可以打印一条或另一条消息。我使用append将结果放入div中,并回显它以显示注释数,但我无法将其与文件中的if语句进行比较 下面是JavaScript: <script> var myVar = setInterval(function(){get_msg_count()}, 1000); function get_msg_count() { $.ajax ({

我正在尝试在我的查看器中实现变量的实时更新,这几乎完成了。问题是我现在想比较变量的结果,这样我就可以打印一条或另一条消息。我使用append将结果放入div中,并回显它以显示注释数,但我无法将其与文件中的if语句进行比较

下面是JavaScript:

<script>
    var myVar = setInterval(function(){get_msg_count()}, 1000);
    function get_msg_count() {
    $.ajax ({
        data: {}, // not really needed
        type: 'POST',
        url:  '<?php echo site_url("blog/getCommentCount");?>', // page to return your msg count
        success: function(response) {
            $('#noOfComments').html(response); 
            }
        }); // End $.ajax
    } // End Function
 </script>

您知道如何将其发送到文件并使用变量吗?谢谢

您必须声明对json数据的响应

控制器

public function getCommentCount()
{
   $comments = new Comment_model();
   $noOfComments = $comments->where('blog_id', '2')->count();
   /**
   *    declare ajax data here
   *    if your $noOfComments will show value
   *    ex : $noOfComments = 10
   */
   $result['comments_count'] = $noOfComments;
   echo json_encode($noOfComments);
}  
还有javascript

success: function(response) {
    $('#noOfComments').html(response.comments_count); 
}
public function getCommentCount()
{
   $comments = new Comment_model();
   $noOfComments = $comments->where('blog_id', '2')->count();
   /**
   *    declare ajax data here
   *    if your $noOfComments will show value
   *    ex : $noOfComments = 10
   */
   $result['comments_count'] = $noOfComments;
   echo json_encode($noOfComments);
}  
success: function(response) {
    $('#noOfComments').html(response.comments_count); 
}