Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7846771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:42:01+00:00 2026-06-02T17:42:01+00:00

hello i have the following problem: i would like to create a filter for

  • 0

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: [ ]

inputfield b: [_____] filter for b: on/off: [ ]

inputfield c: [_____] filter for c: on/off: [ ]

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") :

...
$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:

<?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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:42:03+00:00Added an answer on June 2, 2026 at 5:42 pm

    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.

    <?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 />';
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i have the following problem, I record a video using red5 like this:
I have the following problem: I would like to use footnotes together with the
I have the following regex in PHP: /(?<=\')[^\'\s][^\']*+(?=\')|(?<=)[^\s][^]*+(?=)|[^\',\s]+/ and I would like to port
Hello i am using following xml to create some sort of Transparent panel .
I have the following classes. For testing purpose, I would like to get all
I have some code in the form of a string and would like to
I have the following problem. Let's take the input (wikitext) ======hello((my first program)) world======
Hello I have the following error by git-fsck, which cannot be cleaned by git-gc
Hello I have the following code namespace ConsoleApplication2 { class Program { static void
I have following simple program: import std.stdio; int main(string[] argv) { writeln(Hello, world!); return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.