php - set checkbox checked state based on array values -


let's have bunch of checkbox inputs on page this:

<input type="checkbox" name="vehicle" value="car" /> car<br /> <input type="checkbox" name="vehicle" value="bike" /> bike<br /> <input type="checkbox" name="vehicle" value="motorcycle" /> motorcycle<br /> <input type="checkbox" name="vehicle" value="bus" /> bus<br /> <input type="checkbox" name="vehicle" value="car" /> train<br /> 

now let's have array this:

$my_array = array(     'first' => 'bike',      'second' => 'car',      'third' => 'train' ); 

as can see, array contains values correspond value of checkboxes.

now, how set state of each checkbox input based on value of array , make checkbox stay checked on page refresh using php? in other words, want display checkboxes on page values match value in array should checked on page. possible straight php or need javascript this?

any idea please? have tried no success

sure, can php...

<input type="checkbox" name="vehicle" value="car" <?php if(in_array('car', $my_array)) echo( 'selected = "selected"'); ?>/> car<br /> 

rinse , repeat of checkboxes.

however, smarter if kept array containing possible options. iterate on array , check each entry in array see if exists in 'selected' array.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -