Skip to main content
boloco
Known Participant
December 28, 2009
Answered

PHP Form Validation and Radio Group

  • December 28, 2009
  • 1 reply
  • 830 views

Hi David,

I'm trying to set the initial state of a radio group to none.

Please look at the file http://ecopethandbags.com/contact.php. The initial state is set to "No"

In Dreamweaver, I've set the initial state to "Unchecked"

I don't understand the code for the radio buttons in my file (see attachment).

What I am trying to do is to have the radio group initially unchecked but required.

I have 2 questions:

1) - How can I control the initial status of the radio group when I use the PHP form validation?

2) - How can I set the validation so, when one of the radio buttons is not checked by the user, a warning flag like "Please make a choice" comes up.

Thank you much!

This topic has been closed for replies.
Correct answer David_Powers

Just change the following section of code:

<?php
if (!$_POST || isset($missing) && $_POST['subscribe'] == 'n') {
  echo 'checked="checked"';
} ?>

Remove the !$_POST || like this:

<?php
if (isset($missing) && $_POST['subscribe'] == 'n') {
  echo 'checked="checked"';
} ?>

The checked="checked" will be inserted into the code only if the form has been submitted, but neither radio button has been selected.

1 reply

David_Powers
David_PowersCorrect answer
Inspiring
December 28, 2009

Just change the following section of code:

<?php
if (!$_POST || isset($missing) && $_POST['subscribe'] == 'n') {
  echo 'checked="checked"';
} ?>

Remove the !$_POST || like this:

<?php
if (isset($missing) && $_POST['subscribe'] == 'n') {
  echo 'checked="checked"';
} ?>

The checked="checked" will be inserted into the code only if the form has been submitted, but neither radio button has been selected.

boloco
bolocoAuthor
Known Participant
December 28, 2009

It works!

Thank you much David!