php - 复选框和 Javascript、PHP 的问题

标签 php javascript jquery mysql checkbox

我正在努力使用以下 PHP 和 JavaScript 代码来使用 2 组复选框来过滤从 MySQL 数据库获取的一系列数据。

这是代码:

<script type="text/javascript">
//http://jsbin.com/ujuse/1/edit
$(function() {
    $("input[type='checkbox']").on('change', function() {
        var boxes = [];
        // You could save a little time and space by doing this:
        var name = this.name;
        // critical change on next line
        $("input[type='checkbox'][name='"+this.name+"']:checked").each(function() {
            boxes.push(this.value);
        });
        if (boxes.length) {
            $(".loadingItems").fadeIn(300);
            // Change the name here as well
            $(".indexMain").load('indexMain.php?'+this.name+'=' + boxes.join("+"),
            function() {
                $(".indexMain").fadeIn('slow');
                $(".loadingItems").fadeOut(300);
            });

        } else {
            $(".loadingItems").fadeIn(300);
            $(".indexMain").load('indexMain.php', function() {
                $(".indexMain").fadeIn('slow');
                $(".loadingItems").fadeOut(300);
            });
        }
    });
});
</script>


<?php
function echoCheckboxSet($header, $divClass, $columnName, $setName) {

    include ("connection.php");
$checkboxes = $con -> prepare("SELECT DISTINCT $columnName FROM item_descr ORDER BY $columnName ASC");
$checkboxes->execute();
?>
<div class="bgFilterTitles">
    <h1 class="filterTitles"><?php echo $header;?></h1>
</div>
<div class="<?php echo $divClass; ?>">
<?php
    while ($box = $checkboxes->fetch(PDO::FETCH_ASSOC)):
    $boxColumnName = str_replace('_',' ',$box[$columnName]);
?>
        <input type='checkbox' class='regularCheckbox' name='<?php echo $setName; ?>' value='<?php echo $box[$columnName]; ?>' />
        <font class='similarItemsText'><?php echo $boxColumnName; ?></font>
        <br />
<?php
endwhile;
?>
</div>
<?php
} // end of echoCheckboxSet

// Call our method twice, once for colors and once for prices
echoCheckBoxSet("COLOR", "colors", "color_base1", "color[]");
echoCheckBoxSet("PRICE", "prices", "price", "price[]");
?>

然后我完美地获得了我的复选框,但是当单击其中任何一个时,它们不会执行任何操作。

我的indexMain.php检索如下值:

$colors = $_GET['color[]'];
echo "TEST".$colors[1];
            $colors = explode(' ', $colors);
            $parameters = join(', ', array_fill(0, count($colors), '?'));
            $items = $con -> prepare("SELECT * FROM item_descr WHERE color_base1 IN ({$parameters})");
            $items ->execute($colors);
            $count = $items -> rowCount();

----------------- 添加回显:

echo "<div>Showing ".$count."items</div>";
while($info = $items->fetch(PDO::FETCH_ASSOC)) 
{
echo "<div name='item' id='".$info['color_base1']."' class='itemBox'><div class='showItem'><a href='items_descr.php?itemId=".$info[id_item]."'><img class='itemImage' alt='' src='images/$info[imageMid].jpg'></img></div><br />";
echo "<div class='indexItemText'><font class='similarItemsText'><a href='items_descr.php?itemId=".$info[id_item]."'>".$info[name]."</a><font class='price'> - $".$info[price]."</div></div>";
$row_count++;
if ($row_count % 2 == 0) 
    {
echo "<br />"; // close the row if we're on an even record
    }

}

知道会发生什么吗?

最佳答案

问题是当您在 JS 函数中构建查询时:

'indexMain.php?'+this.name+'=' + boxes.join("+")

这会发送 color[]=Brown+Grey 而不是 color[]=Brown&color[]=Grey。正确(但肮脏)的方法是:

'indexMain.php?'+this.name+'=' + boxes.join('&amp;' + this.name + '=')

您可以尝试使用 jQuery.param() ( http://api.jquery.com/jQuery.param/ ) 来获得更好的代码。

此外,在 PHP 中,复选框值在数组 $_GET['color'] 中可用(而不是 $_GET['color[]'])。

编辑:抱歉,读得太快了。

答案:正如您期望在任何地方都使用字符串一样,请在 JS 和 PHP 代码中使用 color 而不是 color[]

关于php - 复选框和 Javascript、PHP 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14041561/

相关文章:

PHP和文件授权

javascript - 从逗号分隔的字符串中删除重复项

jquery - 如何重叠表格中的背景图像

php - 如何在 Blade 模板中输出 HTML 而不遭受 XSS 攻击?

javascript - Ajax 函数未调用

javascript - 使用 pushsubscriptionchange 时如何获取旧的订阅数据

javascript - 在 Google 图表中显示单位

javascript - 重新加载数据表后无法选择该复选框

javascript - 按换行符拆分返回 "undefined"

php - 使用更新按钮更新 MySQL 字段