简体   繁体   English

如何处理带有语句的复选框并回显它们

[英]how to handle checkboxes with statements and echo them

hello i have the following problem: 你好,我有以下问题:

i would like to create a filter for a search engine. 我想为搜索引擎创建一个过滤器。 i have some inputfields for specific search terms and beside a respective checkbox. 我在特定复选框旁边有一些用于特定搜索词的输入字段。 so that looks like: 这样看起来像:

inputfield a: [ _ ____ ] filter for a: on/off: [ ] 输入字段a:[ _ ____ ]过滤器:a:开/关:[]

inputfield b: [ _ ____ ] filter for b: on/off: [ ] 输入栏b:[ _ ____ ]过滤器b:开/关:[]

inputfield c: [ _ ____ ] filter for c: on/off: [ ] 输入栏c:[ _ ____ ]过滤器,用于c:开/关:[]

the code structure of that is: 其代码结构为:

first of all check if the inputfield is empty and the checkbox is set checked. 首先检查输入字段是否为空,并且复选框已选中。 in case of checked it will be unchecked after submit. 如果选中,则提交后将取消选中。 the other way around, if just the inputfield is filled and the checkbox unchecked, it also will error out, that there is a mistake and the filter can't work. 反之,如果只填写了输入字段而未选中该复选框,则也会出错,即存在错误,并且过滤器无法工作。 so i'm using for each filter an own error array like (the name and value of the first checkbox is name="filter_a" value="1") : 所以我为每个过滤器使用一个自己的错误数组,例如(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';
    }
}

if there is no error and both criteria are fulfilled the filter either is on or not. 如果没有错误,并且两个条件都满足,则筛选器是否打开。 so the logical behind that is, when loading the page the checkbox have to be unchecked. 因此逻辑上就是在加载页面时必须取消选中该复选框。 after regarding the criteria to filter it is on. 在考虑要过滤的条件之后,将其打开。

to display the status checked or unchecked under the conditions below it should be checked after submit or either not. 在以下条件下显示已选中或未选中的状态,应在提交后选中或不选中。 therefor i have this code for each filter (respective checkbox_filter_b, ...) the part of the checkbox after the inputfield: 因此,对于每个过滤器(分别为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"/>';
}
?>

what does not satisfy at all. 什么都不满足。 this is because of the following problem: 这是由于以下问题:

when loading the page it will show all checkboxes are unchecked. 加载页面时,它将显示所有复选框均未选中。 if i try to cause an error because of no filled input or just checked checkbox for one of these filters, after submit all other checkboxes are automatically checked. 如果我由于没有填充输入或仅选中这些过滤器之一的复选框而导致错误,则在提交所有其他复选框后会自动对其进行检查。

so if there is someone who could help me out i really would appreciate. 因此,如果有人可以帮助我,我将不胜感激。 thanks a lot. 非常感谢。

Perhaps you could do it this way, by defining default values for your check boxes and values at the start of your script. 也许可以通过定义复选框的默认值和脚本开头的值来实现。 Then change them with the values of the POSTed if there set or not. 然后使用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 />';
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM