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.

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

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

我正在使用MySQL数据库中的数据设置复选框的ID和名称。例如..

<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; ?>">

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

对于ID为5的条目,它将如下所示..

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

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.

我需要设置它,以便在您选中一个框或取消选中它时它会更新数据库中的“活动”值。如何在单击时获取每个复选框的值,并将该值发送到MySQL数据库。如果我事先知道复选框名称,我可以很容易地做到这一点,但由于名称是从数据库部分生成的,我不知道如何编写代码来确定哪个条目获得活动值。

Here is my jQuery..

这是我的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;
});

Here is the PHP..

这是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();

?>

2 个解决方案

#1


1

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

您可以在输入中添加一个类,并将值设置为id:

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

Then, change your jQuery:

然后,改变你的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;
});

As for PHP:

至于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. :-)

这应该是你正在寻找的。可能会有一些小的语法问题,因为我正在写这篇文章,但我希望你能得到一般的想法。 :-)

更多相关文章

  1. 无法在Yii中更改项目的文件夹名称
  2. PHP / MySQL - 有时会将空白条目添加到表中
  3. MySQL命名约定,字段名称是否应包含表名?
  4. 如何创建一个新表,该表根据名称获取其他表中列的平均值
  5. SQL - 如果存在表条目
  6. 如何知道php脚本中的名称是指mysql表还是mysql视图
  7. 如何使用mysql在php文件夹中快速更改图像名称?
  8. 在时间戳和相应的值中选择每天的最后一个条目。
  9. 具有相同名称的Mutiple按钮显示不同的div。

随机推荐

  1. android中通过拨号键打开contacts相关界
  2. 获取RadioButton选中的值
  3. [置顶] Android Studio Android SDK Andr
  4. Android Animation(API Guides翻译)
  5. 开机自启动3
  6. android 读取联系人
  7. 2.4.11 网格视图和图像切换器
  8. android编译时添加库第三方库文件
  9. 2.4.10 可展开的列表组件
  10. android 蓝牙