Ajax Codeigniter变量问题
我有一个问题,我不知道我是否将其发布在论坛的正确位置。 我正在尝试使用 Ajax 制作一个表单来通过 Ajax 计算我的点击次数。但问题是,每次我点击一次,我总是收到的点击次数为 1。
这是一段解释的代码:
在视图中的 JavaScript
$("#click").click(function(){
$.ajax({
type: "POST",
url: bseUrl+"counter/incCount",
data: click,
success: function(html){
alert(html);
}
});
});
警报(html)应该显示总数来自服务器的点击;
计数器控制器
class Counter extends CI_Controller {
//put your code here
var $numClick;
public function __construct() {
parent::__construct();
$this->numClick= 0;
}
public function Counter() {
parent::__construct();
$this->numClick= 0;
}
public function incCount() {
echo $this->numClick++;
}
public function index() {
//loadView
}
}
但是每次点击时我收到的点击次数总是为 1。 为什么我每次都会丢失可变内容?似乎每次我进行 Ajax 调用时我都会启动该变量。
你们能帮我吗?
I have a question, and I don't know if I'm posting it on the right place of the forum.
I'm trying to do a form with Ajax to count my clicks through Ajax. But the problem is that every time I do a click I always receive the number of clicks as 1.
Here's a piece of the code explained:
In JavaScript from the View
$("#click").click(function(){
$.ajax({
type: "POST",
url: bseUrl+"counter/incCount",
data: click,
success: function(html){
alert(html);
}
});
});
The alert(html) should show the total clicks from the server;
counter controller
class Counter extends CI_Controller {
//put your code here
var $numClick;
public function __construct() {
parent::__construct();
$this->numClick= 0;
}
public function Counter() {
parent::__construct();
$this->numClick= 0;
}
public function incCount() {
echo $this->numClick++;
}
public function index() {
//loadView
}
}
But every time I click I'm always receiving the number of clicks as 1.
Why am I losing the variable content every time? it seems that every time I do a Ajax call I'm starting the variable.
Can you guys help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次调用点击处理函数时都会重新初始化计数器 - 为什么?
尝试这样做:
You are re-initializing the counter every time you call click handling function - why?
try doing it this way: