我想从多个复选框中获取1或0的值,具体取决于是否已选中它们。
我有几行字段
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="">
当字段包含内容时,它们将看起来像这样
Football 90 1 2019-05-09
Cycling 120 0 2019-05-09
Cricket 70 0 2019-05-09
Basketball 90 1 2019-05-09
其中1是复选框已被选中。
我下面有获取值的代码。
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;
}
}
输出不会将复选框的值保持在所选位置,而是将所有所选内容移到顶部。
Football 90 1 2019-05-09
Cycling 120 1 2019-05-09
Cricket 70 0 2019-05-09
Basketball 90 0 2019-05-09
有人可以帮忙吗?
if(isset($_POST['submit'])) {
if (count(array_filter($_POST['duration'])) == count($_POST['duration']) && count(array_filter($_POST['datepicker'])) == count($_POST['datepicker'])) {
foreach ( $_POST['activities'] as $key=>$value ) {
$dist_activity_id = $_POST['activities'][$key];
$dist_activity_duration = $_POST['duration'][$key];
$dist_date = $_POST['datepicker'][$key];
if(isset($_POST['is_distance'][$key])) {
$is_distance = $_POST['is_distance'][$key];
} else {
$is_distance = 0;
}
$dist_list_id = $list_id;
}
}else {
$alert = "<div class='alert alert-danger' role='alert'>Please enter required fields</div>";
}
}