How can I make a radio button for “Other - please specify?”
-
03-07-2019 - |
Question
I am creating an HTML form with some radio button options. I'd like to have one option as "Other - please specify" and allow the user to type something in.
Two questions:
1) How can I make a "hybrid" input type of radio/text
?
2) On the PHP back end, if the input has the same name
attribute as the radio inputs, will the user's input be part of the same array?
Solution
#1: To the "other:" radio field, add a <input type="text" ...>
with style display:none and only display it when user selects the "other:" radio field.
However, I'm not entirely sure if #2 would work. You'd get rboption=other
from the radio button AND rboption=some%20text
from the text field. One will usually overwrite the other, but it's not sure which (read: depends on position in page, browser and phase of the moon).
To be sure, make the textfield name different and only process it when rboption == 'other'
(like Salty said)
OTHER TIPS
Why not just add a different name attribute to the input and only validate it if the other radio button has been selected?
Here's how I did it:
<input type="radio" name="phone" value="313-375-2151">Taylor <br>
<input type="radio" name="phone" value="555-444-1234">OverheadHts <br>
<input type="radio" name="phone" value="555-333-1234">Smith Ctr <br>
<input type="radio" name="phone" value="444-344-1234">Mainsville<br>
<input type="radio" name="phone" value="other">Other:
<input type="text" name="phone-other" size="14">
And then when you process the form:
$phone = mysql_real_escape_string($_POST['phone']);
if ($phone =='other'){
$phone = mysql_real_escape_string($_POST['phone-other']);
}
etc.