Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP - "If, then, else" assistance - Required field:

Status
Not open for further replies.

IKONOS

Technical User
May 2, 2001
20
0
0
US
Good afternoon everyone!

Plain and simple... here's what I need help with:

[fire_chief_name] and [fire_chief_email] need to be required form fields if either [mem-type='alternate'] or [mem_type='delegate'] are selected from the list/menu. If not, neither [fire_chief_name] or [fire_chief_email] need to be required.

Here is a link to the actual form:
... and here is the code:
Code:
</head>


<body>

<table width="748"  border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="112" colspan="3" background="images/scfirefighter_header_logo_i.gif">
	<table width="99%" height="100%"  border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="51%" height="104" valign="bottom"></td>
        <td width="48%" align="center"><?php include('includes/top-navigation.htm'); ?></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td width="150" valign="top" background="images/scfirefighter_header_log-02.gif" align="left"><div class="banner">
        &nbsp;<br>
		<br>
		<span class="style2">
        
        <?php include('includes/left-navigation.htm'); ?>
        
        </span>
        
        
        </div></td >

    <th valign="top" background="images/scfirefighter_header_log-03.gif"> 
      <p class="style1">&nbsp;</p>
      <div align="center"><img src="graphics/SCSFA_color_logo_50.jpg" width="50" height="46"> 
        <font size="3" face="Arial, Helvetica, sans-serif"><strong>Registration 
        South Carolina Fire-Rescue Conference<br>
        </strong><font size="2">The 102nd Annual Conference of the South Carolina 
        State Firefighters' Association<br>
        July 16-21, 2007 - Myrtle Beach Convention Center<br>
        </font></font> </div>
      <hr width="100%" noshade>
      <table width="100%" border="0" cellspacing="0" cellpadding="1">
        <tr>
          <th height="25" bgcolor="#000066">
<div align="center"><font color="#FFFFFF" size="1" face="Arial, Helvetica, sans-serif">&lt; 
              Please complete a seperate Registration Form for each person Attending 
              &gt;</font></div></th>
        </tr>
      </table>
      <div align="center"></div>
      <form name="formmail.php" method="get" action="/php/formmail.php">
        <input type="hidden" name="subject" value="2007_online_conf_reg">
        <input type="hidden" name="required" value="first_name,last_name,rank,address,city,state,zip_code,email,mem_type,fire_dept">
        <input type="hidden" name="env_report" value="REMOTE_HOST,HTTP_USER_AGENT">
        <div align="center"> 
          <table width="100%" border="0" cellpadding="1" cellspacing="2" id="table1">
            <tr> 
              <td align="right"><span class="style9">*</span>First Name: </td>
              <td colspan="-1" align="left"><input name="first_name" type="text" class="input" id="first_name"></td>
            </tr>
            <tr> 
              <td align="right"><span class="style9">*</span>Last Name: </td>
              <td colspan="-1" align="left"><input name="last_name" type="text" class="input" id="last_name"></td>
            </tr>
            <tr> 
              <td align="right">*Rank: </td>
              <td colspan="-1" align="left"><input name="rank" type="text" class="input" id="rank"></td>
            </tr>
            <tr> 
              <td align="right">*Fire Department:</td>
              <td colspan="-1" align="left"><input name="fire_dept" type="text" class="input" id="fire_dept"></td>
            </tr>
            <tr> 
              <td align="right"><span class="style9">*</span>Address:</td>
              <td colspan="-1" align="left"> <input name="address" type="text" class="input" id="address" size="40"></td>
            </tr>
            <tr> 
              <td align="right"><span class="style9">*</span>City:</td>
              <td colspan="-1" align="left"><input name="city" type="text" class="input" id="city"></td>
            </tr>
            <tr> 
              <td align="right"><span class="style9">*</span>State:</td>
              <td colspan="-1" align="left"><input name="state" type="text" class="input" id="state" size="2" maxlength="2"></td>
            </tr>
            <tr> 
              <td align="right"><span class="style9">*</span>Zip Code:</td>
              <td colspan="-1" align="left"><input name="zip_code" type="text" class="input" id="zip_code" size="5" maxlength="5"></td>
            </tr>
            <tr> 
              <td align="right">*Daytime Telephone:</td>
              <td colspan="-1" align="left"> <input name="phone_no" type="text" class="input" id="phone_no" size="18" maxlength="18"></td>
            </tr>
            <tr> 
              <td align="right"><span class="style9">*</span>Email:</td>
              <td colspan="-1" align="left"><input name="email" type="text" class="input" id="email"></td>
            </tr>
            <tr> 
              <td align="right">*Memeber Type:</td>
              <td colspan="-1" align="left"><select name="mem_type" size="1" class="input" id="mem_type">
                  <option value="alternate">Alternate</option>
                  <option value="attendee">Attendee</option>
                  <option value="delegate">Delegate</option>
                  <option value="guest">Guest</option>
                  <option value="member">Member</option>
                </select></td>
            </tr>
            <tr> 
              <td height="15" align="right">&nbsp;</td>
              <td height="15" colspan="-1" align="left">&nbsp;</td>
            </tr>
            <tr bgcolor="#CCCCCC"> 
              <td height="25" colspan="2"> <div align="center">Your Fire Chiefs' 
                  Name and Email address are <u>required</u> if you are attending 
                  this years conference as an <strong>Alternate</strong> or <strong>Delegate</strong></div>
                <div align="center"><br>
                  Fire Chiefs' Name: 
                  <input name="fire_chief_name" type="text" class="input" id="fire_chief_name">
                  <br>
                  <br>
                  Fire Chiefs' Email Address: 
                  <input name="fire_chief_email" type="text" class="input" id="fire_chief_email2">
                </div>
                <br></td>
            </tr>
            <tr> 
              <td height="10" colspan="2" align="right">&nbsp;</td>
            </tr>
            <tr> 
              <td colspan="2" align="right"><div align="center"> 
                  <p><font face="Arial, Helvetica, sans-serif"><strong>H.O.T. 
                    Pre-Conference Seminars</strong><br>
                    Please select <u>one</u> seminar of your choice for <u>each 
                    day</u>.<br>
                    There is limited seating in these programs - Please register 
                    early</font><br>
                </div></td>
            </tr>
          </table>
          <table width="100%">
            <tr> 
              <td height="25" colspan="2"> <div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  </font></div>
                <div align="left"><font size="2" face="Arial, Helvetica, sans-serif"><strong><u>Tue. 
                  - July 17th (9:00AM - 5:00PM)</u></strong></font></div></td>
              <td colspan="2" height="25"> <div align="left"></div>
                <div align="left"><font size="2" face="Arial, Helvetica, sans-serif"><strong><u>Wed. 
                  - July 18th (9:00AM - 5:00PM)</u></strong></font></div></td>
            </tr>
            <tr> 
              <td width="5%"><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  <input name="tue_five_alarm" type="checkbox" class="input" id="tue_five_alarm" value="Yes">
                  </font></div></td>
              <td width="45%"><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  Five-Alarm Leadership </font></div></td>
              <td width="5%"><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  <input name="wed_five_alarm" type="checkbox" class="input" id="wed_five_alarm2" value="Yes">
                  </font></div></td>
              <td width="45%"><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  Five-Alarm Leadership </font></div></td>
            </tr>
            <tr> 
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  <input name="tue_fireground" type="checkbox" class="input" id="tue_fireground2" value="Yes">
                  </font></div></td>
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  Firegound Academy </font></div></td>
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  <input name="wed_ventilation" type="checkbox" class="input" id="wed_ventilation2" value="Yes">
                  </font></div></td>
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  Ventilation 2007 </font></div></td>
            </tr>
            <tr> 
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  <input name="tue_res_tactics" type="checkbox" class="input" id="tue_res_tactics2" value="Yes">
                  </font></div></td>
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  Residential Tactics </font></div></td>
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  <input name="wed_res_tactics" type="checkbox" class="input" id="wed_res_tactics2" value="Yes">
                  </font></div></td>
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  Residential Tactics </font></div></td>
            </tr>
            <tr> 
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  <input name="tue_no_return" type="checkbox" class="input" id="tue_no_return2" value="Yes">
                  </font></div></td>
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  The Point of No Return </font></div></td>
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  <input name="wed_no_return" type="checkbox" class="input" id="wed_no_return2" value="Yes">
                  </font></div></td>
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  The Point of No Return </font></div></td>
            </tr>
            <tr> 
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  <input name="tue_trapped_ff" type="checkbox" class="input" id="tue_trapped_ff2" value="Yes">
                  </font></div></td>
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  Rescuing Trapped Firefighters </font></div></td>
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  <input name="wed_trapped_ff" type="checkbox" class="input" id="wed_trapped_ff2" value="Yes">
                  </font></div></td>
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  Rescuing Trapped Firefighters </font></div></td>
            </tr>
            <tr> 
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  <input name="tue_killer_attic" type="checkbox" class="input" id="tue_killer_attic3" value="Yes">
                  </font></div></td>
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  Killer in the Attic </font></div></td>
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  <input name="wed_iso" type="checkbox" class="input" id="wed_iso2" value="Yes">
                  </font></div></td>
              <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> 
                  Understanding ISO Grading Schedule </font></div></td>
            </tr>
          </table>
        </div>
        <div align="center"> <span class="style9"><font size="2" face="Arial, Helvetica, sans-serif">* 
          </font></span><font size="2" face="Arial, Helvetica, sans-serif"><span class="style7"> 
          Denotes a Required Field</span></font></p> <br>
          <input type="submit" name="Submit" value="Submit">
          <input type="reset" name="Submit2" value="Reset"></p>
          </div>
        <div align="center">&nbsp;</div>
        <input type=hidden name="recipient" value="Mark Lutton">
        <input type=hidden name="subject" value="2007 SC Fire-Rescue Conference Online Registration">
        <input type="hidden" name="redirect" value="[URL unfurl="true"]http://scfirefighters.org/2007_online_thanks.php">[/URL]
        <input type=hidden name="print_blank_fields" value="1">
      </form></th>
    <td width="148" align="center" valign="top" background="images/scfirefighter_header_log-04.gif" >
	<?php include('includes/right-navigation.htm'); ?></td>
  </tr>
  <tr valign="bottom">
    <td height="145" colspan="3"  background="images/scfirefighter_header_log-05.gif"><?php include('includes/footer.htm'); ?></td>
  </tr>
</table>
</body>
</html>

Any assistance is greatly appreciated!
Mark
 
for a proof of concept see the below:

Code:
<?php
$field1 = isset($_POST['field1']) ? trim ($_POST['field1']) : "";
$memtype = isset($_POST['memtype']) ? trim($_POST['memtype']) : "";	
$error = NULL;

if (isset($_POST['submit'])){
	switch ($memtype){
		case "one":
			//field1 required
			if (empty($field1)) {
				$error[] = "You must complete field 1";
			}
			break;
		case "two":
			//
			break;
		default:
			//
	}
	
	if (!is_null($error)){
		$error= "<div class=\"error\">". implode("<br/>", $error) . "</div>";
		displayForm($error);
	} else {
		echo "form properly submitted";
		//do something with it
	}
} else {
	displayForm();
}

function displayForm($error=Null){
global $field1;
?>
<style> .error{color:red;}</style>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<fieldset>
	<?=$error?>
	Type:	<select name="memtype">
				<option value="one">One</option>
				<option value="two">Two</option>
			</select><br/>
	Potentially Required Field: <input type="text" name="field1" value="<?=$field1?>" /><br/>
	<input type="submit" name="submit" value="submit" />
</fieldset>
</form>
<?php
}?>
 
You need to check fist whether mem_type is one of the 2 options. and then check whether the two fields you need are filled.

example:
Code:
if(($_POST['mem_type']=='alternate')||($_POST['mem_type']=='delegate')){
if(($_POST['fire_chief_name']!='')&&$_POST['fire_chief_email']!='')){
\\[green]continue with processing of form[/green]
}
else{[green]\\Either one of those is not filled in. [/green]}
}

else{
\\[green]Since the mem_type is neither of those options, the 2 fields in question require no validation for emptiness move one with the rest of the form validation.. [/green]
}

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top