How to dynamically mark checkbox fields with PHP?

Asked

Viewed 1,018 times

-1

I’m sending neighborhoods by $_GET through a form and would like to mark them dynamically in updating the screen thus making a system of persistence of the search results.

<fieldset class="normal">
    <input type="checkbox" name="bairros[]" value="BELEM NOVO" id="BELEM NOVO">
    <label for="BELEM NOVO">Belém Novo</label>  
    <input type="checkbox" name="bairros[]" value="BOM FIM" id="BOM FIM">
    <label for="BOM FIM">Bom Fim</label>
    <input type="checkbox" name="bairros[]" value="SANTA CLARA" id="SANTA CLARA">
    <label for="SANTA CLARA">Santa Clara</label>
</fieldset>

I include the file bairros.php shortly after the body with a require and capture all the neighborhoods that were marked on the form with $_GET on the return page.

# Persistencia dos bairros
$inbairros = $_GET["bairros"];
if(array_key_exists("bairros", $_GET)){     
    foreach($inbairros as $baitem => $bavalue) {
        // select bairros       
    }   
}

If I do echo $bavalue within the foreach, logically it returns me all the neighborhoods that were selected in the form. The value input contains the same content.

Question: How to dynamically select returned neighborhoods and place checked in all of them without having to treat one by one?

  • That’s on a search form and you want to keep those neighborhoods checked as you choose at the time of the search?

  • Marcos, do these neighborhoods.php files include your form? It needs to be generated dynamically or do you have to use Javascript.

  • Jorge is a research form and I want to keep the fields checked after the Submit of it (persistence of the fields).

  • Gmsantos, the bairro.php contains simply the foreach code added just after the body that sees if house Submit from the specific array and takes the data.

1 answer

1

Boy... what a confusing question...

First of all, this here:

$inbairros = $_GET["bairros"];
if(array_key_exists("bairros", $_GET)){     
    foreach($inbairros as $baitem => $bavalue) {
        // select bairros       
    }   
}

It should be at least:

$bairros = ( array_key_exists( 'bairros', $_GET ) ? $_GET['bairros'] : array() );

foreach( $bairros as $bairro ) {

    // Faz alguma coisa
}

But that still doesn’t solve your problem. To solve we should know how this foreach relates to the HTML itself.

Usually something like this is done:

// Apenas um exemplo, isso viria do banco

$bairros = array( 'bairro1', 'bairro2', 'bairro3', 'bairro4', 'bairro5' );

foreach( $bairros as $bairro ) {
    echo '<input type="checkbox" name="bairros[]" value="' . $bairro . '" id="' . $bairro . '">';
    echo '<label for="' . $bairro . '">' . $bairro . '</label> ';
}

And to persist the checkboxes selected, something like that:

// Apenas um exemplo, isso viria do banco

$bairros = array( 'bairro1', 'bairro2', 'bairro3', 'bairro4', 'bairro5' );

// Apenas um exemplo, isso seria o $_GET

$bairrosSelecionados = array( 'bairro3', 'bairro4' );

foreach( $bairros as $bairro ) {

    $checked = ( in_array( $bairro, $bairrosSelecionados ) ? ' checked="checked" ' : ' ' );

    echo '<input type="checkbox" name="bairros[]"' . $checked . 'value="' . $bairro . '" id="' . $bairro . '">';
    echo '<label for="' . $bairro . '">' . $bairro . '</label> ';
}

Read, understand and adapt your needs. ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.