如何使用Ajax来更新MySQL数据库复选框时,情况发生改变? [英] How to use ajax to update mysql db when checkbox condition is changed?

查看:166
本文介绍了如何使用Ajax来更新MySQL数据库复选框时,情况发生改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文章对客户端的行显示的表。每个物品都有一个唯一的ID,并包含一个复选框,表示如果这篇文章被选中为收藏。如果它是一个最喜欢的,则该复选框已被选取。如果不是,它被选中。现在,我需要或者JS或jQuery和Ajax来更新该表在数据库中,如果当复选框病情变化具体到每一行。另一个挑战是,我在CakePHP的MVC环境下我的工作。

 <脚本类型=文/ JavaScript的SRC =jQuery的-1.2.1.min.js>< / SCRIPT>
<脚本类型=文/ JavaScript的>

    功能checkbox_click(ID,最喜欢的)
    {
        //看是否复选框被选中
        如果(喜爱== 1)
        {
            $阿贾克斯({
                键入:POST,
                网址:check_favorite.php',//这个外部的PHP文件不连接到MySQL数据库
                数据:'ID ='+ ID +'&放大器;放大器;最喜欢= ​​1',
            });
        }// 如果
        //该复选框是选中
        其他
        {
            $阿贾克斯({
                键入:POST,
                网址:check_favorite.php',//这个外部的PHP文件不连接到MySQL数据库
                数据:'ID ='+ ID +'&放大器;放大器;最喜欢= ​​0',
            });
        }//其他
    }
< / SCRIPT>
 

- html--这是在一个foreach循环

 回声<输入类型=复选框ID ='$的rowid;' NAME ='喜欢'检查='检查'的onclick ='checkbox_click('身份证','最爱',这();/>中;


其他
回声<输入类型=复选框ID ='$的rowid;' NAME ='喜欢'的onclick ='checkbox_click('身份证','最爱',this.checked); />中;
 

- 称为AJAX的php文件 -

 < PHP
//数据库变量 - 与变量输入不连接
$ DBHOST ='localhost'的; //通常是本地主机
$ DBUSER ='用户名'; //数据库用户名
$ DBPASS =密码; //数据库密码

//建立连接到MySQL数据库

$ CON = @mysql_connect($ DBHOST,$ DBUSER,$ DBPASS);
如果(!$ CON)
死亡(无法连接。mysql_error());

mysql_select_db('devcake',$ CON);

//获取变量。
$查询=更新mytable的设置喜爱=。$ _ POST ['喜欢'。 
WHERE ID =$ _ POST ['身份证']。;;

的mysql_query($查询);
则mysql_close($ CON);
?>
 

解决方案

这是在codeI使用(感谢萨穆埃尔)

  $('输入[名称=最爱]')。住(点击,函数(){
    VAR ID = $(本).attr('身份证');

    如果($(本).attr('检查')){
        VAR最喜欢= ​​1;
    } 其他 {
        变种喜爱= 0;
    }

    $阿贾克斯({
        键入:GET,
        网址:favorites.php,
        数据:'ID ='+ ID +'和;最喜欢='+最爱
    });
    //console.log('id:'+ ID +'Público有:+ +检察署'值:'+值);

 });
 

I have a table of articles that displays in rows on client side. Each articles has a unique id and contains a checkbox to indicate if this article is checked as a favorite. If it is a favorite, then the checkbox is already checked. If not, it is unchecked. Now I need either js or jquery and ajax to update the table in the DB if and when the checkbox condition changes specific to each row. Another challenge is that I am working in cakePHP MVC environment.

<script type="text/javascript" src="jquery-1.2.1.min.js"></script>
<script type="text/javascript">

    function checkbox_click (id, favorite)
    {
        // see if checkbox is checked
        if(favorite==1)
        {
            $.ajax({
                type:'POST',
                url:'check_favorite.php', // this external php file isn't connecting to mysql db
                data:'id= ' + id + '&amp;favorite=1',
            });
        }// if
        // the checkbox was unchecked
        else
        {
            $.ajax({
                type:'POST',
                url:'check_favorite.php', // this external php file isn't connecting to mysql db
                data:'id= ' + id + '&amp;favorite=0',
            });
        }//else
    }
</script>

--html-- this is within a foreach loop.

echo "<input type='checkbox' id='$rowid;' name='favorite' checked='checked' onclick='checkbox_click('id','favorite',this();' />";


else
echo "<input type='checkbox' id='$rowid;' name='favorite'  onclick='checkbox_click('id','favorite',this.checked);' />";

--php file called by ajax--

<?php
//Database Variables - with the variables entered it doesn't connect
$dbhost = 'localhost';   // usually localhost
$dbuser = 'username';      // database username
$dbpass = 'password';      // database password

//Establish connection to MySQL database

$con = @mysql_connect($dbhost, $dbuser, $dbpass);
if (!$con)
die('Unable to connect.' . mysql_error());

mysql_select_db('devcake', $con);

// Get the variables.
$query = "UPDATE mytable SET favorite=".$_POST['favorite'] . "
WHERE id=".$_POST['id'] . ";";

mysql_query($query);
mysql_close($con);
?>

解决方案

This is the code i use (thanks to samuel)

 $('input[name=favorite]').live("click",function(){
    var id    = $(this).attr('id');

    if($(this).attr('checked')) {
        var favorite = 1;
    } else {
        var favorite = 0;
    }

    $.ajax({
        type:'GET',
        url:'favorites.php',
        data:'id= ' + id + '&favorite='+favorite
    });
    //console.log('id: ' + id + ' Publico: '+publico + 'Value: '+value);

 });

这篇关于如何使用Ajax来更新MySQL数据库复选框时,情况发生改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆