Can we post Unchecked Radio Button value?
Asked Answered
F

3

6

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?

Follower answered 21/3, 2014 at 11:42 Comment(5)
You could check the unchecked radio button with javascript just before sending the form.Fugacity
I do need checked value but i also need unchecked value.Follower
suppose you have 4 radio buttons. and if you check any one so other radio button you can't check. Then which radio is check take as check value else radio value take as unchecked.Ornamental
Why do you need the unchecked value?Exaggerate
Of course you can "send" unchecked value. It simply won't be in your superglobal $_POST/$_GET/$_REQUEST since it won't be propagated.Salcido
F
1

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.

Follower answered 22/3, 2014 at 13:53 Comment(0)
T
10
<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.

Tuyettv answered 21/3, 2014 at 12:1 Comment(0)
A
1

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>
Al answered 21/3, 2014 at 12:15 Comment(0)
F
1

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.

Follower answered 22/3, 2014 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.