Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Validation of Radio Button that includes Text Box

Community Beginner ,
Jan 18, 2010 Jan 18, 2010

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.

TOPICS
Server side applications
1.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 19, 2010 Jan 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 20, 2010 Jan 20, 2010

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


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 20, 2010 Jan 20, 2010
LATEST

You know, after reading your suggestion, I reread it about three times and then went back to your PHP Solutions book, and I finally got it right! WHEW!

The one think I couldn't get was when you said "Then your validation problem should become clearer." It took awhile, but I finally got it and I'm so proud of myself, thanks to you by the way. I remembered that I had to chang 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 } ?>

to this:

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

Whew! Things worked wonderfully! Now I just need to test this with a checkbox option...

Thank you again, Mr. Powers!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines