In my html form, I have a group of radio buttons
<form action="receive.php" method="post">
<input type="radio" name="rad" value="one" /> One <br />
<input type="radio" name="rad" value="two" /> Two <br />
<input type="radio" name="rad" value="three" /> Three <br />
<input type="submit" value="submit" />
</form>
I have a scenario where no radio button is checked. In such case, my server-side php receives input value as false
. What I want to do is change false to null
or undefined
. Is that possible? Any help is greatly appreciated.
In my html form, I have a group of radio buttons
<form action="receive.php" method="post">
<input type="radio" name="rad" value="one" /> One <br />
<input type="radio" name="rad" value="two" /> Two <br />
<input type="radio" name="rad" value="three" /> Three <br />
<input type="submit" value="submit" />
</form>
I have a scenario where no radio button is checked. In such case, my server-side php receives input value as false
. What I want to do is change false to null
or undefined
. Is that possible? Any help is greatly appreciated.
- which server side language ur referring to ? – swapnesh Commented Oct 29, 2012 at 10:29
4 Answers
Reset to default 8You can do something like this:
<form action="receive.php" method="post">
<input type="radio" name="rad" value="one" /> One <br />
<input type="radio" name="rad" value="two" /> Two <br />
<input type="radio" name="rad" value="three" /> Three <br />
<input type="hidden" name="rad" value="null" />
<input type="submit" value="submit" />
</form>
Now if you haven't checked any radio button, you will get "null" input value, but remember "null"
is not same as NULL
in PHP.
Radio buttons, by definition, do not have a null value.
You could however add a radio button such as this:
<form action="receive.php" method="post">
<input type="radio" name="rad" value="one" /> One <br />
<input type="radio" name="rad" value="two" /> Two <br />
<input type="radio" name="rad" value="three" /> Three <br />
<input type="radio" name="rad" value="null" /> null <br />
<input type="submit" value="submit" />
</form>
And use it as the null that you need.
That might be done with an if/else statement within the .php:
$radioInput = false;
if (!isset($_POST['rad'])){
$radioInput = NULL;
}
else {
$radioInput = $_POST['rad'];
}
http://php/manual/en/function.isset.php
Use javascript to return null in case if no radio button is selected ,on "submit" click :
var radioChecked=0;
var radios=document.getElementsByName("rad");
for(var i=0;i<radios.length;i++)
{ if(radios[i].checked){
radioChecked=radioChecked+1;
} }
if(radioChecked==0)
return null;
Hope it solves your problem.