我有使用JavaScript更改值的复选框,我想在PHP数组中存储所有复选框的值。但是如果用户将一个复选框的值更改为" 0",则创建的数组只有2个值(1,1)而不是(1,0,1)。
HTML
$args = array(
'post_type' => 'progetto',
'posts_per_page' => -1
);
// Query the posts:
$grid_post_types_query = new WP_Query($args);
if ($grid_post_types_query->have_posts()) {
$grid_img = '';
$grid_details = '';
$i = 1;
while ($grid_post_types_query->have_posts()) : $grid_post_types_query->the_post();
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'progetti');
$url = $image_url[0];
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'full');
$url2 = $image_url[0];
$content = wpautop(get_the_content());
$grid_img .= ' <li class="gridder-list" data-griddercontent="#gridder-content-' . $i . '"> <img src="' . $url . '" class="img-responsive" /></li>';
$grid_details .= '<div id="gridder-content-' . $i . '" class="gridder-content">
<div class="row">
<div class="col-sm-6"> <img src=" ' . $url2 . '" class="img-responsive" />
here i would like have repeater image </div>
<div class="col-sm-6"> ' . $content . ' </div>
</div>
</div>';
$i++;
endwhile;
echo $output = '<div class="container griddercontainer"><ul class="gridder"> ' . $grid_img . '</ul>' . $grid_details . ' </div>';
}
wp_reset_postdata();
wp_reset_query();
PHP
<input type="checkbox" name="cbox1[]" checked="checked" value="1" class="chbo">
<input type="checkbox" name="cbox1[]" checked="checked" value="1" class="chbo">
<input type="checkbox" name="cbox1[]" checked="checked" value="1" class="chbo">
Jquery的
$wer=$_POST['cbox1'];
echo implode(",",$wer);
答案 0 :(得分:1)
仅提交成功(已选中)控件。您需要在复选框之前添加具有默认值的隐藏输入。请注意,您需要对索引进行硬编码,以使隐藏的默认值与复选框值匹配:
<input type="hidden" name="cbox1[0]" value="0" class="chbo">
<input type="checkbox" name="cbox1[0]" value="1" class="chbo" checked="checked">
<input type="hidden" name="cbox1[1]" value="0" class="chbo">
<input type="checkbox" name="cbox1[1]" value="1" class="chbo" checked="checked">
因此,如果未选中,则只会提交值为cbox1[0]
的隐藏0
。如果选中,则值为cbox1[0]
的{{1}}将覆盖隐藏的输入。
您现在不需要JavaScript,因为您可以将复选框更改为值1
,但如果未选中则不会提交。
答案 1 :(得分:0)
表单提交后,POST或GET数组中不存在未选中的复选框。要检查未选中的复选框表单元素,请使用简单的isset()调用。
$wer[0]=isset($_POST['cbox1'][0]) ? $_POST['cbox1'][0]:0;
$wer[1]=isset($_POST['cbox1'][1]) ? $_POST['cbox1'][1]:0;
...