New to this forum, but thought I would give it a try, here's the problem:

Radio button on a form, if the user does NOT select anything, I need to show
an error message. If the user selects yes on this radio button, I need to
make sure a specific field is filled in, if not, show an error message
related to the text field, but NOT the error message from the radio button.
Selecting NO, requires nothing.

Here's the code I came up with:

if(!isset($_POST['LegalResidencelessthan1yr'])){
$d=true;
if(eregi("[a-z | A-Z]+", $_POST['FormerCity'])){
$d=true;
}else{
$d=false;
$message[]="Please enter a valid former city, ex., <span 
class=\"error\">Springfield</span>";
}
if(eregi("[a-z | A-Z]+", $_POST['FormerState'])){
$d=true;
}else{
$d=false;
$message[]="Please enter a valid former state, ex., <span 
class=\"error\">MO</span>";
}
if(eregi("[a-z | A-Z]+", $_POST['FormerSchoolDist'])){
$d=true;
}else{
$d=false;
$message[]="Please enter a valid former school district, ex., <span 
class=\"error\">Springfield R-12</span>";
}
  }
else{
$d=false;
$message[]="Please select whether you have lived at your legal residence 
less than one year.";
 }

Legal residence is the radio button. Former City, Former State and Former
School Dist are the text fields that are required after selecting yes. To me
this looks like it should work. The text field(s) validation does work, the
radio button does not. If anyone can spot a problem let me know or needs
further info I would gladly provide it.

    You can always use:

    if(array_key_exists('LegalResidencelessthan1yr', $_POST))
    {
      // The radio button has been selected (either yes or no)
    }
    else
    {
      // The radio button has not been selected
    }

      Well, I've actually came up with this:

      if($_POST['LegalResidencelessthan1yr']=='yes'){
      if(eregi("[a-z | A-Z]+", $_POST['FormerCity'])){ 
      $d=true; 
      }else{ 
      $d=false; 
      $message[]="Please enter a valid former city, ex., <span class=\"error\">Springfield</span>"; 
      } 
      if(eregi("[a-z | A-Z]+", $_POST['FormerState'])){
      $d=true;
      }else{
      $d=false;
      $message[]="Please enter a valid former state, ex., <span class=\"error\">MO</span>";
      }
      if(eregi("[a-z | A-Z]+", $_POST['FormerSchoolDist'])){
      $d=true;
      }else{
      $d=false;
      $message[]="Please enter a valid former school district, ex., <span class=\"error\">Springfield R-12</span>";
      }
        } 
      else{ 
      $d=false; 
      $message[]="Please select whether you have lived at your legal residence less than one year."; 
       }
      

      Just need to figure out how to handle the no selection, since nothing is required.

        Well, that's where my code comes in...

        If you do an array_search for 'LegalResidencelessthan1yr' it will return TRUE or FALSE. If it returns TRUE, then the radio button has been selected. If not, then it hasn't been selected.

        If it returns true, you then look for what the value of the radio button would be.

        The code you have would result in an error with not having an index in the $_POST array if the radio button is not selected.

          Write a Reply...