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