How get value for unchecked checkbox in checkbox e

2020-01-25 06:38发布

I have a form like below :

<form action="" method="post">
    <input type="checkbox" id="status_1" name="status_1" value="1" />
    <input type="checkbox" id="status_2" name="status_2" value="1" />
    <input type="checkbox" id="status_3" name="status_3" value="1" />
</form>

When i check all checkbox and post the form, the result is like this:

Array ([status_3] => 1 [status_2] => 1 [status_1] => 1 ) 

Then i uncheck second checkbox and post the form, the result is like this:

Array ( [status_3] => 1 [status_1] => 1 ) 

Is it possible to make result like this below when i uncheck second checkbox :

Array ( [status_3] => 1 [status_2] => 0 [status_1] => 1 ) 

There are ideas to do it?

标签: php arrays forms
11条回答
劫难
2楼-- · 2020-01-25 06:44

First way - hidden fields (disadvantage: the user can manipulate the value of the field (but one can manipulate the value of the checkbox too, so it's not really a problem, if you only expect 1 or 0))

<form action="" method="post">
<input type="hidden" name="status_1" value="0" />
<input type="checkbox" id="status_1" name="status_1" value="1" />
<input type="hidden" name="status_2" value="0" />
<input type="checkbox" id="status_2" name="status_2" value="1" />
<input type="hidden" name="status_3" value="0" />
<input type="checkbox" id="status_3" name="status_3" value="1" />
<input type="submit" />
</form>
<?php
var_dump($_POST);
/*
 * checking only the second box outputs:
 * 
 * array (size=3)
  'status_1' => string '0' (length=1)
  'status_2' => string '1' (length=1)
  'status_3' => string '0' (length=1)
 */

Second way - to assign default value for non-set indexes:

<form action="" method="post">
<input type="checkbox" id="status_1" name="status_1" value="1" />
<input type="checkbox" id="status_2" name="status_2" value="1" />
<input type="checkbox" id="status_3" name="status_3" value="1" />
<input type="submit" />
</form>
<?php
for($i = 1; $i<=count($_POST); $i++) {
    $_POST["status_$i"] = isset($_POST["status_$i"]) ? $_POST["status_$i"] : 0;
}
var_dump($_POST);

/**
 * Here we will be checking only the third checkbox:
 * 
 * array (size=3)
  'status_3' => string '1' (length=1)
  'status_1' => int 0
  'status_2' => int 0
 */
查看更多
在下西门庆
3楼-- · 2020-01-25 06:44
<!--html code-->                                                                  
<input type="checkbox" name="correct" value="1">Option 1
<input type="checkbox" name="correct" value="2">Option 2
<input type="checkbox" name="correct" value="3">Option 3
<input type="checkbox" name="correct" value="4">Option 4                                   

//php code in function called on form submit       
  public function addOptions(Request $request)
   {
    $option = array('1' => 0,'2'=>0,'3'=>0,'4'=>0 );
    $option[$request->correct] = 1;
    return $option;
   }
查看更多
老娘就宠你
4楼-- · 2020-01-25 06:45

Assuming we are using checkboxes with zeros or ones...

Using a hidden checkbox with a zero value is just a work-around. Another work around would be to add 0 to the value when receiving the post or get.

Example:

$chkbx1 = $_POST['chckbx1']; $chkbx1 += 0;

This takes a NULL value an turns it into a zero, but if the value is one, as in its checked, then the value stays the same.

The real issue here isn't inventing a work-around. Its understanding why this is happening. Older versions of mySQL takes null values and converts them into a zero. In newer versions, you must disable strict mode and then a work-around is not needed.

查看更多
Juvenile、少年°
5楼-- · 2020-01-25 06:53

I thinks it impossible to get array like what you want from html forms. But this some tricks can be used:

$defaultForm = array(
'status_1' => 0,
'status_2' => 0,
'status_3' => 0, 
);

// example array from $_POST
$form = array(
'status_1' => 1,
'status_3' => 1, 
);

$form = array_merge($defaultForm, $form);

Result:

array(3) {

'status_1' => int(1)
'status_2' => int(0)
'status_3' => int(1)

}

查看更多
手持菜刀,她持情操
6楼-- · 2020-01-25 06:57

the question may already be answered but i just wanted to take a stab at it...server side only solution:

$p = $_POST;
$a = array();
$a['status_3'] = (int) ($p['status_3'] === 1);
$a['status_2'] = (int) ($p['status_2'] === 1);
$a['status_1'] = (int) ($p['status_1'] === 1);

Testing

 // if input is Array("status_1"=>1) output will be
 Array ( [status_1] => 1 [status_3] => 0 [status_2] => 0 )

 // if input is Array("status_1"=>1, "status_2"=>1) output will be
 Array ( [status_1] => 1 [status_3] => 0 [status_2] => 1)
查看更多
等我变得足够好
7楼-- · 2020-01-25 07:02

Thanks all. Thank to @RoyalBg give me solution. Like this :

<input type="hidden" name="status_1" value="0" />
<input type="checkbox" id="status_1" name="status_1" value="1" /> Check 1 <br />
<input type="hidden" name="status_2" value="0" /> 
<input type="checkbox" id="status_2" name="status_2" value="1" /> Check 2 <br />
<input type="hidden" name="status_3" value="0" />
<input type="checkbox" id="status_3" name="status_3" value="1" /> Check 3 <br />

It's work perfectly.. :)

查看更多
登录 后发表回答