Conditional and Required Fields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dmwesq
Forum Newbie
Posts: 21
Joined: Wed Jan 23, 2008 4:35 pm

Conditional and Required Fields

Post by dmwesq »

I'm not sure exactly what to call this issue, and I'm sure part of it is an HTML issue as well, but what I am looking to do is take a form I created, and have some of the fields only visible based on the answer to the previous question on the form. However, when the field is visible, I would like to set it as a required field.

As an example, this form has 2 yes/no questions regarding medical issues. If the answer is no, then the details box which follows each would not appear on the form. If the answer is yes, then we would want the details filled in. For the transport box, if the answer is yes, then I have a follow up question; if the answer is no, there will be a different follow up question which also may require a further follow up depending on the answer.

I hope I have explained this clearly. I also have one other small issue - I'm trying to echo the entry in the variable 'activity' in the response back to the user so they no for sure which activity they submitted the form for, but no matter how I've tried it does not seem to work - so what am I doing wrong there?

Code: Select all

<?php

$activity = $_POST['activity'];
$activity_date = $_POST['activitydate'];
$location = $_POST['location'];
$scout_name = $_POST['scoutname'];
$attending = $_POST['attending'];
$parent = $_POST['parent'];
$email = $_POST['email'];
$primary_phone = $_POST['primaryphone'];
$alternate_phone = $_POST['alternatephone'];
$alternate_contact = $_POST['alternatecontact'];
$alternate_contact_phone = $_POST['alternatecontactphone'];
$physical = $_POST['physical'];
$other = $_POST['other'];
$medicine = $_POST['medicine'];
$other2 = $_POST['other2'];
$transport = $_POST['transport'];
$scouts = $_POST['scouts'];
$transport2 = $_POST['transport2'];
$other3 = $_POST['other3'];
$legal = $_POST['legal'];
$additional = $_POST['additional'];
if (!isset($_POST['activity'])) {

?>

Code: Select all

<form method="post" action="permission5.php">
    
    <div class="form-all">
        <ul class="form-section">
            <li class="form-line" id="id_1">
                <label class="form-label-left" id="label_1" for="input_1"> Activity<span class="form-required">*</span> </label>
                <div id="cid_1" class="form-input">
                <input type="text" class="form-textbox" id="input_1" name="activity" size="30" />  
                </div>
            </li>
            <li class="form-line" id="id_2">
                <label class="form-label-left" id="label_2" for="input_2"> Date(s): </label>
                <div id="cid_2" class="form-input">
                    <input type="text" class="form-textbox" id="input_2" name="activitydate" size="30" />
                </div>
            </li>
            <li class="form-line" id="id_3">
                <label class="form-label-left" id="label_3" for="input_3"> Location: </label>
                <div id="cid_3" class="form-input">
                    <input type="text" class="form-textbox" id="input_3" name="location" size="30" />
                </div>
            </li>
            <li class="form-line" id="id_4">
                <label class="form-label-left" id="label_4" for="input_4"> Scout Name(s):<span class="form-required">*</span> </label>
                <div id="cid_4" class="form-input">
                    <input type="text" class="form-textbox" id="input_4" name="scoutname" size="30" />
                </div>
            </li>
            <li class="form-line" id="id_5">
                <label class="form-label-left" id="label_5" for="input_5"> List any other people attending: </label>
                <div id="cid_5" class="form-input">
                    <input type="text" class="form-textbox" id="input_5" name="attending" size="30" />
                </div>
            </li>
            <li class="form-line" id="id_6">
                <label class="form-label-left" id="label_6" for="input_6"> Parent/Guardian Name:<span class="form-required">*</span> </label>
                <div id="cid_6" class="form-input">
                    <input type="text" class="form-textbox" id="input_6" name="parent" size="30" />
                </div>
            </li>
			<li class="form-line" id="id_20">
                <label class="form-label-left" id="label_20" for="input_20"> E-mail: <span class="form-required">*</span> </label>
                <div id="cid_20" class="form-input">
                    <input type="text" class="form-textbox" id="input_20" name="email" size="35" />
                </div>
            </li>
            <li class="form-line" id="id_7">
                <label class="form-label-left" id="label_7" for="input_7"> Primary Phone: <span class="form-required">*</span> <span class="style3">(Format xxx-xxx-xxxx)</span></label>
                <div id="cid_7" class="form-input">
                    <input type="text" class="form-textbox" id="input_7" name="primaryphone" size="20" />
                </div>
            </li>
            <li class="form-line" id="id_8">
                <label class="form-label-left" id="label_8" for="input_8"> Alternate Phone: </label>
                <div id="cid_8" class="form-input">
                    <input type="text" class="form-textbox" id="input_8" name="alternatephone" size="20" />
                </div>
            </li>
            <li class="form-line" id="id_9">
                <label class="form-label-left" id="label_9" for="input_9"> Alternate Contact: </label>
                <div id="cid_9" class="form-input">
                    <input type="text" class="form-textbox" id="input_9" name="alternatecontact" size="30" />
                </div>
            </li>
            <li class="form-line" id="id_10">
                <label class="form-label-left" id="label_10" for="input_10"> Alternate Contact Phone:</label>
                <div id="cid_10" class="form-input">
                    <input type="text" class="form-textbox" id="input_10" name="alternatecontactphone" size="20" />
                </div>
            </li>
            <li class="form-line" id="id_11">
                <label class="form-label-left" id="label_11" for="input_11"> Are there any changes to your child's (or your own) medical condition since his last BSA Medical Form was submitted?<span class="form-required">*</span> </label>
                <div id="cid_11" class="form-input">
                    <div class="form-multiple-column"><span style="clear:left;">
                      <input type="radio" class="form-radio" id="input_11_0" name="physical" value="yes" />
                            <label for="input_11_0"> yes </label></span><span class="clearfix"></span><span style="clear:left;"><input type="radio" class="form-radio" id="input_11_1" name="physical" value="no" />
                            <label for="input_11_1"> no </label></span><span class="clearfix"></span>
                  </div>
                </div>
            </li>
            <li class="form-line" id="id_12">
                <label class="form-label-left" id="label_12" for="input_12"> Describe changes: </label>
               
                    <textarea id="input_12" class="form-textarea" name="other" cols="30" rows="6"></textarea>
                
            </li>
            <li class="form-line" id="id_13">
                <label class="form-label-left" id="label_13" for="input_13"> Are there any changes to your child's (or your own) medications (including over the counter drugs) since his last BSA Medical Form was submitted?<span class="form-required">*</span> </label>
                <div id="cid_13" class="form-input">
                    <div class="form-multiple-column"><span style="clear:left;">
                      <input type="radio" class="form-radio" id="input_13_0" name="medicine" value="yes" />
                            <label for="input_13_0"> yes </label></span><span class="clearfix"></span><span style="clear:left;"><input type="radio" class="form-radio" id="input_13_1" name="medicine" value="no" />
                            <label for="input_13_1"> no </label></span><span class="clearfix"></span>
                  </div>
                </div>
            </li>
            <li class="form-line" id="id_14">
                <label class="form-label-left" id="label_16" for="input_14"> Describe changes: </label>
                <div id="cid_14" class="form-input">
                    <textarea id="input_14" class="form-textarea" name="other2" cols="30" rows="6"></textarea>
                </div>
            </li>
            <li class="form-line" id="id_15">
                <label class="form-label-left" id="label_15" for="input_15"> Is your scout being transported to and/or from the activity by a parent or guardian?<span class="form-required">*</span> </label>
                <div id="cid_15" class="form-input">
                    <div class="form-multiple-column"><span style="clear:left;"><input type="radio" class="form-radio" id="input_15_0" name="transport" value="yes" />
                            <label for="input_15_0"> yes </label></span><span class="clearfix"></span><span style="clear:left;"><input type="radio" class="form-radio" id="input_15_1" name="transport" value="no" />
                            <label for="input_15_1"> no </label></span><span class="clearfix"></span>
                    </div>
                </div>
            </li>
			<li class="form-line" id="id_16">
                <label class="form-label-left" id="label_16" for="input_16"> If yes, how many other scouts can you transport? </label>
                <div id="cid_16" class="form-input">
                   <select class="form-dropdown" style="width:40px" id="input_16" name="scouts">
                         
                        <option value="0"> 0 </option>
                        <option value="1"> 1 </option>
                        <option value="2"> 2 </option>
                        <option value="3"> 3 </option>
                        <option value="4"> 4 </option>
                    </select>
                </div>
				
            <li class="form-line" id="id_17">
                <label class="form-label-left" id="label_17" for="input_17"> If no, have you arranged for transportation with another troop member? </label>
                <div id="cid_17" class="form-input">
                    <div class="form-multiple-column"><span style="clear:left;"><input type="radio" class="form-radio" id="input_17_0" name="transport2" value="yes" />
                            <label for="input_17_0"> yes </label></span><span class="clearfix"></span><span style="clear:left;"><input type="radio" class="form-radio" id="input_17_1" name="transport2" value="no" />
                            <label for="input_17_1"> no </label></span><span class="clearfix"></span>
                    </div>
                </div>
            </li>
            <li class="form-line" id="id_18">
                <label class="form-label-left" id="label_18" for="input_18"> If yes, provide name of family who will be transporting </label>
                <div id="cid_18" class="form-input">
                    <input type="text" class="form-textbox" id="input_18" name="legal" size="20" />
                </div>
            </li>
			  <li class="form-line" id="id_21">
                <label class="form-label-left" id="label_21" for="input_14"> List any additional information: </label>
                <div id="cid_21" class="form-input">
                    <textarea id="input_21" class="form-textarea" name="additional" cols="30" rows="6"></textarea>
                </div>
            </li>
                            
                    
            <li class="form-line" id="id_19"><input type="checkbox" class="form-checkbox" id="input_19_0" name="legal" value="Agreed" /><span class="form-required">*</span>
                <label class="form-label-all" id="label_19" for="input_19"> As the parent or legal guardian of the above named scout(s) I hereby give my permission for my son(s) to participate in the above-mentioned activity. I give permission to the leaders of the above unit, to render first aid, should the need arise. In the event of an emergency, I also give permission to the adult leader in charge, to secure proper medical treatment, and for the medical provider(s) so selected to hospitalize, secure proper anesthesia, order injection, or secure other medical treatment, as needed. I further agree to hold the above named unit and its leaders blameless for any accidents that might occur during this outing except for clear acts of negligence or non-adherence to BSA policies and guidelines. </label>
               
                
            </li>
            <li class="form-line" id="id_22">
                <div id="cid_22" class="form-input-wide">
                    <div style="text-align:center" class="form-buttons-wrapper">
                        <button id="input_22" type="submit" class="form-submit-button">
                            Submit Form
                        </button>
                    </div>
                </div>
           
</form>

Code: Select all

<?php
}
else {
  

mail( "info@***.org", "Permission Form",
"Activity: $activity\n
Activity Date: $activity_date\n
Location: $location\n
Scout Name: $scoutname\n
Others Attending: $attending\n
Parent/Guardian: $parent\n
Email: $email\n
Primary Phone: $primaryphone\n
Alternate Phone: $alternatephone\n
Alternate Contact: $alternatecontact\n
Alternate Contact Phone: $alternatecontactphone\n
Any Medical Change Since Last Physical: $physical\n
Describe Changes: $other\n
Any change in medications: $medicine\n
Describe Changes: $other2\n
Is Parent/Guardian Transporting: $transport\n
If yes, number of other scouts you can transport: $scouts\n
If not, have transport arrangements been made: $transport2\n
Name of family transporting: $other3\n
Legal Acknowledgement: $legal\n
List any additional information: $additional",
	"From: $email" );
  
    echo  'Thanks for submitting the permission form for $activity.';
}
?>
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

Re: Conditional and Required Fields

Post by spedula »

If you want the second question to appear without a page refresh/form submit, then this can only be done on the client side, JavaScript.

Your second issue. You can only echo $_POST data after it's been posted, form submit. You can get the value of an input field through JavaScript without a form submit.

If the data has already been posted and you're trying to echo the var in the middle of some HTML you have to enclose it in <?PHP ?> tags.

Hope this helps a bit.
dmwesq
Forum Newbie
Posts: 21
Joined: Wed Jan 23, 2008 4:35 pm

Re: Conditional and Required Fields

Post by dmwesq »

After some playing around, I managed to get not only the variable to reflect in the echo - which I thought could be done as it had already posted by the time it was called, but I also have a delayed redirect working as well. And I will look at javascript as well as some other php examples I found on other sites to resolve the first issue. I have some other questions but since they deal with other topics I'll create a new post for that.
Post Reply