Can we post Unchecked Radio Button value?

这一生的挚爱 提交于 2019-12-03 15:49:25
<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.

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>

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.

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