Can we post Unchecked Radio Button value?

主宰稳场 提交于 2019-12-04 22:11:04

问题


I have a situation where i do need unchecked radio button value. Is it possible to send unchecked radio button value through form in php? If yes, then can you please explain?


回答1:


<input type="hidden" name="myField" value="false" />
<input type="checkbox" name="myField" value="true" />

both fields have the same name, if the checkbox isn't checked, the hidden field will be submitted, if the checkbox is checked, it will virtually override the the hidden field.

On a side note, are your sure radio buttons wouldn't be better suited to your needs, you will be able to see the definite answer for each questions, whereas checkboxes are more for submitted only the selected checkboxes. Failing that you could always assume that all checkboxes that were not submitted as checked, could be assumed to be unchecked.




回答2:


Create sample.php file and paste below code and run it.

<?php
echo "<pre>";
if(isset($_POST['numbers']) && isset($_POST['unchecked']))
{
  $checked_items = $_POST['numbers'];
  $unchecked_items = array_diff($_POST['unchecked'],$_POST['numbers']);
  echo 'Checked Item<br/>';print_r($checked_items);
  echo '<br/><br/>Unchecked Items<br/>';print_r($unchecked_items);
}
?>

<form name='frmname' action='' method='post'>
<input type='radio' name='numbers[]' value='one'/>One
<input type='radio' name='numbers[]' value='two' />Two
<input type='radio' name='numbers[]' value='three' />Three

<input type='hidden' name='unchecked[]' value='one' />
<input type='hidden' name='unchecked[]' value='two' />
<input type='hidden' name='unchecked[]' value='three' />

<input type='submit' name='submit' value='Submit' />
</form>



回答3:


I have solved it. I have sent both radio button values in hidden field and as well as radio button values. On receiving page, I have compare checked value with hidden field value and so I got unchecked value. Thanks for all who responded to my question.



来源:https://stackoverflow.com/questions/22557804/can-we-post-unchecked-radio-button-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!