Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Keep Checkbox State on Match Array Keys After Submit

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
doge
Smarty n00b


Joined: 03 Dec 2019
Posts: 3

PostPosted: Thu Dec 05, 2019 2:31 am    Post subject: Keep Checkbox State on Match Array Keys After Submit Reply with quote

I want to keep the checkbox state checked after I submit the form. I've made this code

For show all checkbox options.

Code:
$arr_array = array();
foreach( explode( ',', 'red,blue,green,black' ) as $k => $v )
{
    $arr_array[] = array(
        "name" => trim($v),
        "selected" => ''
    );
}
$smarty->assign('arr_array', $arr_array);


And in template I created an If statement to check if value "selected=1" then checked else unchecked.


Code:

<div class="form-group row">
      <label for="method-input" class="col-md-2 col-form-label form-control-label" required>Color Choose:</label>
      <div class="col-md-10">
        <div class="row px-3">
        {section name=p loop=$arr_array}
        <div class="custom-control custom-checkbox mb-3 col">
          <input class="custom-control-input" id="custom{$arr_array[p].name}" name="colors[{$arr_array[p].name}]" value="1" type="checkbox" {if $arr_array[p].selected}checked{/if}>
          <label class="custom-control-label text-white font-weight-bolder" for="custom{$arr_array[p].name}">{$arr_array[p].name}</label>
        </div>
        {/section}
        </div>
      </div>
    </div>


Also I created this function to check which options was checked

Code:
foreach( $arr_array as $key => $value ){
     $keys = array_keys($_POST['colors']);
}


How to keep the state of checkbox after submit the form?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP