更新通过AJAX / PHP MySQL中值的动态创建的复选​​框,单击时? [英] Update value in MySQL via AJAX/PHP when a dynamically created checkbox is clicked?

查看:118
本文介绍了更新通过AJAX / PHP MySQL中值的动态创建的复选​​框,单击时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个管理面板,我工作的一个项目。它会列出一堆条目表中的每一行都会有它的复选框。此复选框将用于激活被在网站上显示的条目。

I am creating an admin panel for a project that I am working on. It will list a bunch of entries in a table and each row will have a checkbox in it. This checkbox will be used to activate an entry to be displayed on the website.

我设置了​​ ID 名称从MySQL数据库数据的复选框。例如..

I am setting the id and name of the checkbox with data from the MySQL database. For example..

<input type="checkbox" class="active" name="active<?php echo $id; ?>" id="active<?php echo $id; ?>" <?php if ($active == 1): ?>checked="checked"<?php endif; ?> value="<?php echo $id; ?>">

有关的条目的5号会是这样的。

For the entry with ID of 5 it will look like this..

<input type="checkbox" class="active" name="active5" id="active5" checked="checked" value="5">

我需要设置成当你选中一个复选框,或者取消选中它更新数据库中的活动的价值。我如何抓住每个复选框,点击后的值,该值发送给MySQL数据库。我可以做到这一点很容易,如果我知道复选框名称事前,但由于名称部分是从数据库中生成的我也不太清楚怎么写code,以确定哪些项得到积极的价值。

I need to set this up so that when you check a box or uncheck it that it updates the "active" value in the database. How do I grab the value of each checkbox, when clicked, and send that value to the MySQL database. I can do this easily if I know the checkboxes name beforehand, but since the name is partly generated from the database I'm not sure how to write the code to determine which entry gets the active value.

这是我的jQuery ..

$("input.active").click(function() {
// store the values from the form checkbox box, then send via ajax below
var check_active = $(this).is(':checked') ? 1 : 0;
var check_id = $(this).attr('value');

console.log(check_active);
console.log(check_id);

    $.ajax({
        type: "POST",
        url: "http://nowfoods.marketspacecom.com/nextstep/ajax.php",
        data: {id: check_id, active: check_active},
        success: function(){
            $('form#submit').hide(function(){$('div.success').fadeIn();});

        }
    });
return true;
});

下面是PHP ..

<?php

include("dbinfo.inc.php");
mysql_connect($server,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"); 

// CLIENT INFORMATION
$active = mysql_real_escape_string($_POST['active']);
$id = mysql_real_escape_string($_POST['id']);

$addEntry = "UPDATE entries SET active = '$active' WHERE id = '$id'";
mysql_query($addEntry) or die(mysql_error());


mysql_close();

?>

推荐答案

您可以添加一个类来输入和设定值的ID而不是:

You could add a class to the input and set the value to the id instead:

&LT;输入类=主动式=复选框名称=active5ID =active5值=5检查=检查&GT;

然后,改变你的jQuery的:

Then, change your jQuery:

$("input.active").click(function() {
// store the values from the form checkbox box, then send via ajax below
var check_active = $(this).is(':checked') ? 1 : 0;
var check_id = $(this).attr('value');

    $.ajax({
        type: "POST",
        url: "http://nowfoods.marketspacecom.com/nextstep/ajax.php",
        data: {id: check_id, active: check_active}
        success: function(){
            $('form#submit').hide(function(){$('div.success').fadeIn();});

        }
    });
return true;
});

对于PHP:

As for PHP:

<?php

include("dbinfo.inc.php");
mysql_connect($server,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"); 

// CLIENT INFORMATION
$active        = mysql_real_escape_string($_POST['active']);
$id = mysql_real_escape_string($_POST['id']);

// WHERE id=16 is just for testing purposes. Need to dynamically find which checkbox was checked and use that info to tell the query which ID to update. 
$addEntry  = "UPDATE entries SET active = '$active' WHERE id = '$id'";
mysql_query($addEntry) or die(mysql_error());

mysql_close();
?>

这应该是你要找的内容。可能会有一些小的语法问题,因为我在写这离我的头顶部,但我希望你得到的总体思路。 : - )

This should be what you're looking for. There may be some minor syntax issues, because I'm writing this off the top of my head, but I hope you get the general idea. :-)

这篇关于更新通过AJAX / PHP MySQL中值的动态创建的复选​​框,单击时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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