vendredi 10 mai 2019

Get 1 or 0 value of multiple checkboxs depending if they are checked

I want to get either a 1 or 0 value from multiple checkboxes depending on if they are checked or not.

I have rows of fields

activity    duration [checkbox] date
activity    duration [checkbox] date
activity    duration [checkbox] date
activity    duration [checkbox] date

<select name="activities[]">
    <option value="12">Football</option>
</select>
<input type="text" name="duration[]" placeholder="" /> 
<input type="checkbox" name="is_distance[]" value="1">
<input type="text" class="datepicker" name="datepicker[]" id="">

When the fields have content they will look something like this

Football   90  1 2019-05-09
Cycling    120 0 2019-05-09
Cricket    70  0 2019-05-09
Basketball 90  1 2019-05-09

where 1 is the checkbox has been checked.

I have the code below that gets the values.

foreach ( $_POST['activities'] as $key=>$value ) {
    $dist_activity_id = $_POST['activities'][$key];
    $dist_activity_duration = $_POST['duration'][$key];
    $dist_date = $_POST['datepicker'][$key];
    $dist_list_id = $list_id;

    if(isset($_POST['is_distance'][$key])) {
        $is_distance = $_POST['is_distance'][$key];
    } else {
        $is_distance = 0;
    }
}

The output doesn't keep the checkbox values in the positions there where selected, all the selected moved to the top.

Football   90  1 2019-05-09
Cycling    120 1 2019-05-09
Cricket    70  0 2019-05-09
Basketball 90  0 2019-05-09

Could someone please help with this?




Aucun commentaire:

Enregistrer un commentaire