CakePHP 和 jQuery 按钮点击计数器
我写了一个小的 jQuery 按钮点击计数器,我有点卡住了,因为我想提交我的表单以向 mysql 数据库(cakephp)添加一行,然后如果添加了则更新页面上的值,但如果它没有添加到数据库,计数器不会上升。
我被困在如何将值返回到 jquery 来说明它是否成功插入到数据库中。
这是我目前正在使用的代码。
js:
$(document).ready(function()
{
$('.add-vote-link').click( function ()
{
var img_id = $(this).attr('id');
$.ajax({
type: "POST",
url: "/votes/vote/" + img_id,
// data: "img_id="+ img_id,
success: function()
{
//$('form#submit').hide(function(){$('div.success').fadeIn();});
$('#vote_counter_'+img_id).html(function(i, val) { return +val+1 });
}
});
});
});
要更新的html:
<div id="vote_counter_<?php echo $img['Images']['img_id']; ?>">
<?php echo $img['Images']['img_vote_count']; ?>
</div>
php用于将数据提交到数据库,这也可以正常工作,如果成功插入则返回1,如果没有插入则返回0,我如何将其传递回我的jquery函数,然后有一个if 语句是否执行计数器 ++。
希望有人可以帮忙,iv 解释一下就可以了:)
谢谢!
I have written a little jQuery button click counter, i have got a little stuck as i want to submit my form to add a row to a mysql db (cakephp) and then if it is added update the value on the page, but if it doesnt get added to the db the counter doesnt go up.
I am stuck on the how to return a value back to jquery to say it was a successful insert into the db or not.
Here is the code i am using at the moment.
js:
$(document).ready(function()
{
$('.add-vote-link').click( function ()
{
var img_id = $(this).attr('id');
$.ajax({
type: "POST",
url: "/votes/vote/" + img_id,
// data: "img_id="+ img_id,
success: function()
{
//$('form#submit').hide(function(){$('div.success').fadeIn();});
$('#vote_counter_'+img_id).html(function(i, val) { return +val+1 });
}
});
});
});
html to update:
<div id="vote_counter_<?php echo $img['Images']['img_id']; ?>">
<?php echo $img['Images']['img_vote_count']; ?>
</div>
php is used to submit the data to a db, this works fine also and either returns 1 for a successful insert and 0 if it didnt insert, how do i pass this back to my jquery function and then have an if statement to do the counter ++ or not.
Hope someone can help and iv explained it ok :)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果一切都很完美,你只需要以 json 形式传递数据:D
在页面“/votes/vote/”+ img_id 中,你应该让它显示 json 数据,
视图最像这样
$results 应该是一个数组像这样
,你的布局应该是这样的
最后你的$.ajax应该是这样的
像这样你可以在cakephp页面执行后将任何数据传递到你的jquery,希望这对你有用:D(我从记忆所以也许我错过了一些东西:S)
If everything is perfect, you only need is to pass the data as json :D
In the page "/votes/vote/" + img_id you should put that it displays json data
the view most be something like this
$results should be a array like this
and your layout for this should be something like this
finally your $.ajax should be something like this
Like this you may pass any data to your jquery after the cakephp page execution, hope this works for you :D (I did this from memory so maybe i'm missing something :S)