views:

698

answers:

3

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?

+8  A: 

Why not just add a different name attribute to the input and only validate it if the other radio button has been selected?

Salty
+6  A: 

#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)

Piskvor
Cool. I will make the optional text field appear with Javascript, so to make sure it degrades gracefully, I think I will have it display by default and hide it with Javascript unless it's needed.
Nathan Long
+1  A: 

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.