CakePHP 和 jQuery 按钮点击计数器

发布于 2024-12-11 00:42:48 字数 1050 浏览 2 评论 0原文

我写了一个小的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡的优雅 2024-12-18 00:42:48

如果一切都很完美,你只需要以 json 形式传递数据:D

在页面“/votes/vote/”+ img_id 中,你应该让它显示 json 数据,

视图最像这样

<?php
echo $javascript->object($results);
?>

$results 应该是一个数组像这样

$results = array( 'result' => 1, //any other data you want to pass
);

,你的布局应该是这样的

<?php
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate");
header('Content-Type: text/x-json');
echo $content_for_layout;
?>

最后你的$.ajax应该是这样的

$.ajax({  
            type: "POST",  
            url: "/votes/vote/" + img_id,  
        //  data: "img_id="+ img_id,
            dataType: "json",  
            success: function( data ){  
                if (data.result == 1){
                     counter++;
                } else {
                  //display error message or something
                }
                $('#vote_counter_'+img_id).html(function(i, val) { return +val+1 });
            }  
});

像这样你可以在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

<?php
echo $javascript->object($results);
?>

$results should be a array like this

$results = array( 'result' => 1, //any other data you want to pass
);

and your layout for this should be something like this

<?php
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate");
header('Content-Type: text/x-json');
echo $content_for_layout;
?>

finally your $.ajax should be something like this

$.ajax({  
            type: "POST",  
            url: "/votes/vote/" + img_id,  
        //  data: "img_id="+ img_id,
            dataType: "json",  
            success: function( data ){  
                if (data.result == 1){
                     counter++;
                } else {
                  //display error message or something
                }
                $('#vote_counter_'+img_id).html(function(i, val) { return +val+1 });
            }  
});

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文