Skip to main content
Known Participant
January 19, 2010
질문

Validation of Radio Button that includes Text Box

  • January 19, 2010
  • 1 답변
  • 1001 조회

Ok. I have a brief form that has a variety of radio buttons and text boxes. Validating text boxes and checkboxes aren't a problem. My problem is validating radio buttons that have an 'Other:' option that includes a textbox.

For example, I have this:

<label for="hearDog">2. How did you hear about this particular dog(s)?</label> <?php if (isset($error['hearDog'])) { ?>
<span class="warning"><?php echo $error['hearDog']; ?></span>
<?php } ?>
<br />

<input name="hearDog" type="radio" id="Petfinder.org" value="Petfinder.org" <?php
    if (!$_POST || (isset($error) && $_POST['hearDog'] == 'Petfinder.org')) { echo
    'checked="checked"';
} ?>    />
<label for="Petfinder.org">Petfinder.org</label>

<input name="hearDog" type="radio" value="Pets911" id="Pets911" <?php
    if (isset($error) && $_POST['hearDog'] == 'Pets911') { echo
    'checked="checked"';
} ?>    />
<label for="Pets911">Pets911</label>

<input name="hearDog" type="radio" value="Other" id="Other" <?php

    if (!$_POST || (isset($error) && $_POST['hearDog'] == 'Other')) { echo
    'checked="checked"';
} ?>    />
<label for="Other">Other, please explain:</label>
<br />
<textarea name="hearDog" cols="50" rows="7"><?php if (isset($_POST['hearDog'])) {echo $_POST['hearDog'];} ?></textarea>

The radio button validation I currently have is this:

    $hearDog = $_POST['hearDog'];
    if (empty($hearDog)) {
    $error['hearDog'] = 'Please select how you heard about this dog(s)';
    }

My problem is that if someone ops to choose 'Other', but doesn't fill in the textarea, how do I validate the text area and pop the message 'Please select...'. Plus, how do I create create a 'Sticky Radio Button'? I haven't had any luck doing that during my tests.

Thank you for your time.

이 주제는 답변이 닫혔습니다.

1 답변

David_Powers
Inspiring
January 19, 2010

There are several problems with your code. The way you have set up the radio group results in checked="checked" being inserted in both Petfinder.org and Other. As a result, Other is selected by default when the page is loaded. You need to remove the !$_POST || from the conditional statement of one or both of them. Leave it in the value you want to make the default, or if you don't want a default, remove it from both.

Next, if (empty($hearDog)) will never be true for a radio group. If a radio group value is selected, its value will be in the $_POST array. If no value is selected (because you don't have a default), the radio group's name will not appear in the $_POST array.

That leads me on to your third problem. You have given the text area the same name as your radio group. Call it something like "hearOther". Then your validation problem should become clearer.

$hearOther = trim($_POST['hearOther']);

if (isset($_POST['hearDog']) && $_POST['hearDog'] == 'Other' && empty($hearOther)) {

  $error['hearOther'] = 'Please explain how your heard about this dog';

}

When redisplaying the value in the text area, you should pass it to htmlentities() to avoid any problems with the output.

toad78작성자
Known Participant
January 21, 2010

I assume that this would be used the same way if a checkbox was used?