【问题标题】:how to handle checkboxes with statements and echo them如何处理带有语句的复选框并回显它们
【发布时间】:2012-04-26 05:38:09
【问题描述】:

你好,我有以下问题:

我想为搜索引擎创建一个过滤器。我有一些用于特定搜索词的输入字段,并且在相应的复选框旁边。所以看起来像:

输入字段 a:[_____] 过滤器:开/关:[]

输入字段 b:[_____] 过滤器 b:开/关:[]

输入字段 c:[_____] 过滤器 c:开/关:[]

其代码结构为:

首先检查输入字段是否为空并且复选框是否设置为选中状态。如果选中,提交后将取消选中。反之,如果只填写了输入字段并且未选中复选框,它也会出错,即出现错误并且过滤器无法工作。所以我为每个过滤器使用一个自己的错误数组,如(the name and value of the first checkbox is name="filter_a" value="1")

...
$checkbox_filter_a = $db->real_escape_string(htmlentities(trim($_POST['filter_a'])));
...
$filter_a = (!empty($checkbox_filter_a)) ? 1 : 0;
...
$errors_a = array();
if ($filter_a == 1){
    if (empty($input_a)){
        $errors_a[] = 'the filter needs some input';
    }
}

if (!empty($input_a)){
    if ($filter_a == 0){
        $errors_a[] = 'filter is not activated';
    }
}

如果没有错误并且两个条件都满足,过滤器要么打开,要么不打开。 所以这背后的逻辑是,在加载页面时,必须取消选中复选框。在关于过滤它的条件之后。

在以下条件下显示已选中或未选中的状态,应在提交后选中或不选中。因此,我为每个过滤器(分别为 checkbox_filter_b,...)在输入字段后的复选框部分提供了此代码:

<?php
if (checkbox_filter_a == 0 ) {
     echo '<input type="checkbox" name="checkbox_filter_a" value="1" checked/>';
}else {
     echo '<input type="checkbox" name="checkbox_filter_a" value="1"/>';
}
?>

什么都不满足。这是因为以下问题:

加载页面时,它会显示所有复选框都未选中。如果我尝试由于没有填写输入或仅选中其中一个过滤器的复选框而导致错误,则在提交后会自动选中所有其他复选框。

所以如果有人可以帮助我,我真的很感激。非常感谢。

【问题讨论】:

    标签: php mysql html variables checkbox


    【解决方案1】:

    也许您可以这样做,方法是在脚本开始时为复选框和值定义默认值。然后用 POSTed 的值更改它们(如果有设置)。

    <?php
    //Setup Variable Defaults, if POST dont change them then there no complicated ifelse's
    $filter_a=null;
    $checkbox_filter_a='<input type="checkbox" name="checkbox_filter_a" value="1"/>';
    $filter_b=null;
    $checkbox_filter_b='<input type="checkbox" name="checkbox_filter_b" value="1"/>';
    $filter_c=null;
    $checkbox_filter_c='<input type="checkbox" name="checkbox_filter_c" value="1"/>';
    
    //Check for post
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        //If the check box is not checked checkbox_filter_a will fail this part
        // so $filter_a will still be null 
        if(isset($_POST['filter_a']) && isset($_POST['checkbox_filter_a'])){
            $filter_a = trim($_POST['filter_a']);
            $checkbox_filter_a ='<input type="checkbox" name="checkbox_filter_a" value="1" checked/>';
        }
    
        if(isset($_POST['filter_b']) && isset($_POST['checkbox_filter_b'])){
            $filter_b = trim($_POST['filter_b']);
            $checkbox_filter_b = '<input type="checkbox" name="checkbox_filter_b" value="1" checked/>';
        }
    
        if(isset($_POST['filter_c']) && isset($_POST['checkbox_filter_c'])){
            $filter_c = trim($_POST['filter_c']);
            $checkbox_filter_c = '<input type="checkbox" name="checkbox_filter_c" value="1" checked/>';
        }
    
    }
    
    echo <<<FORM
     <form method="POST" action="">
       <p>Inputfield a: <input type="text" name="filter_a" value="$filter_a" size="20"> filter for a: on/off:$checkbox_filter_a</p>
       <p>Inputfield b: <input type="text" name="filter_b" value="$filter_b" size="20"> filter for b: on/off:$checkbox_filter_b</p>
       <p>Inputfield c: <input type="text" name="filter_c" value="$filter_c" size="20"> filter for c: on/off:$checkbox_filter_c</p>
       <p><input type="submit" value="Submit"></p>
     </form>
    FORM;
    
    //now here all you have to do is see if these values are not null and build your query
    echo $filter_a.'<br />';
    echo $filter_b.'<br />';
    echo $filter_c.'<br />';
    ?>
    

    【讨论】:

    • 您好,感谢您的回答,我刚刚测试过,但它在回显复选框本身时遇到了问题。它们不会显示出来。
    • @bonny 我不确定你的意思,看看它在这里工作:codepad.org/cFjKBvKh
    • 你好,对不起。我今天有点忙。好吧,我发现了错误。 html中仍然有旧代码。我遇到的另一件事是为每个设置错误数组:我使用: if ( ($input_a === null) && (isset($_POST['checkbox_filter_a'])) ){ $errors_1[] = "filter需要输入”; }else if ( ($input_a !== null) && (!isset($_POST['checkbox_filter_a'])) ){ $errors_1[] = "过滤器未激活"; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    相关资源
    最近更新 更多