简体   繁体   中英

PHP Form Array and Autofill

The Form itsef is for a office referral form, i have it bulit pretty much compltely just trying to add , along with what i currently have working, the ability to check more then one checkbox at a time and be bale to send them using the mail(); function in PHP. So it needs to either have it concatinated onto a variable or i was reading something about an array with name='infraction[]' using the brackets like that in the name but couldnt quite figure it out hwo to work with both the auto fill back in using <?php if (isset($_POST['Infraction']) && $_POST['Infraction']=='Disorderly Conduct') echo "checked";?> . Any help is much appriciated
Thank you in advance, Cheers.
here is some of the code that it needs to work with:

 @charset "utf-8"; /* CSS Document */ body { background-color: #d5d5d5; /*top colour*/ background-image: -webkit-linear-gradient(top, #d5d5d5, #fdfa8b); background-image: -moz-linear-gradient(top, #d5d5d5, #fdfa8b); background-image: -o-linear-gradient(top, #d5d5d5, #fdfa8b); background-image: linear-gradient(to bottom, #d5d5d5, #fdfa8b); } #registration-form { font-family: 'Open Sans Condensed', sans-serif; width: 600px; min-width: 250px; margin: 20px auto; position: relative; } #registration-form .fieldset { background-color: #d5d5d5; border-radius: 3px; } #registration-form legend { text-align: center; background: #364351; width: 100%; padding: 30px 0; border-radius: 3px 3px 0 0; color: white; font-size: 2em; } .fieldset form { border: 1px solid #2f2f2f; margin: 0 auto; display: block; width: 100%; padding: 30px 20px; box-sizing: border-box; border-radius: 0 0 3px 3px; } .placeholder #registration-form label { display: none; } .no-placeholder #registration-form label { margin-left: 5px; position: relative; display: block; color: grey; text-shadow: 0 1px white; font-weight: bold; } .fieldSelect { text-shadow: 1px 1px 1px white; font-weight: bold; } /* all */ ::-webkit-input-placeholder { text-shadow: 1px 1px 1px white; font-weight: bold; } ::-moz-placeholder { text-shadow: 0 1px 1px white; font-weight: bold; } /* firefox 19+ */ :-ms-input-placeholder { text-shadow: 0 1px 1px white; font-weight: bold; } /* ie */ #registration-form input[type=text] { padding: 15px 20px; border-radius: 1px; margin: 5px auto; background-color: #f7f7f7; border: 1px solid silver; -webkit-box-shadow: inset 0 1px 5px rgba(0, 0, 0, 0.2); box-shadow: inset 0 1px 5px rgba(0, 0, 0, 0.2), 0 1px rgba(255, 255, 255, .8); width: 100%; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; -ms-box-sizing: border-box; box-sizing: border-box; -webkit-transition: background-color .5s ease; -moz-transition: background-color .5s ease; -o-transition: background-color .5s ease; -ms-transition: background-color .5s ease; transition: background-color .5s ease; } .no-placeholder #registration-form input[type=text] { padding: 10px 20px; } #registration-form select { padding: 15px 20px; border-radius: 1px; margin: 5px auto; background-color: #f7f7f7; border: 1px solid silver; -webkit-box-shadow: inset 0 1px 5px rgba(0, 0, 0, 0.2); box-shadow: inset 0 1px 5px rgba(0, 0, 0, 0.2), 0 1px rgba(255, 255, 255, .8); width: 100%; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; -ms-box-sizing: border-box; box-sizing: border-box; -webkit-transition: background-color .5s ease; -moz-transition: background-color .5s ease; -o-transition: background-color .5s ease; -ms-transition: background-color .5s ease; transition: background-color .5s ease; } .no-placeholder #registration-form select { padding: 10px 20px; } #registration-form input[type=text]:active, .placeholder #registration-form input[type=text]:focus { outline: none; border-color: silver; background-color: white; } #registration-form input[type=submit] { font-family: 'Open Sans Condensed', sans-serif; text-transform: uppercase; outline: none; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; -ms-box-sizing: border-box; box-sizing: border-box; width: 100%; background-color: #5C8CA7; padding: 10px; color: white; border: 1px solid #3498db; border-radius: 3px; font-size: 1.5em; font-weight: bold; margin-top: 5px; cursor: pointer; position: relative; top: 0; } #registration-form input[type=submit]:hover { background-color: #2980b9; } #registration-form input[type=submit]:active { background: #5C8CA7; } .parsley-error-list { background-color: #C34343; padding: 5px 11px; margin: 0; list-style: none; border-radius: 0 0 3px 3px; margin-top: -5px; margin-bottom: 5px; color: white; border: 1px solid #870d0d; border-top: none; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; position: relative; top: -1px; text-shadow: 0px 1px 1px #460909; font-weight: 700; font-size: 12px; } .parsley-error { border-color: #870d0d!important; border-bottom: none; } #registration-form select { width: 100%; padding: 10px 20px; } ::-moz-focus-inner { border: 0; } .err { color: red; font-size: 16px; font-weight: bold; } 
 <legend>Office Form</legend> <form action="<?php $_SERVER['PHP_SELF'];?>" method="POST"> <div style="text-align: right;" class='row'> <label>Date:</label> <span><?php echo date("m/d/Y"); ?></span> <label>Time:</label> <span><?php echo date("h:i:sa"); ?></span> <br> </div> <div class='row'> <label>Students Name<span class="err"><?php echo $Err1; ?></span> </label> <input autofocus type="text" name="Student_Name" placeholder="Enter Student Name" value="<?php echo htmlspecialchars($_POST['Student_Name']); ?>" /> </div> <div class='row'> <span class="err"><?php echo $Err3; ?></span> <select name="Class"> <option <?php if($_POST[ 'Class']==0){echo 'selected="true"';} ?>value="0"><span class="fieldSelect"> Auburn Program </span> </option> <option <?php if($_POST[ 'Class']=='(ADM)Advanced Manufacturing' ){echo 'selected="true"';} ?>value="(ADM)Advanced Manufacturing">(ADM)Advanced Manufacturing</option> <option <?php if($_POST[ 'Class']=='(AHT)Allied Health Technology' ){echo 'selected="true"';} ?>value="(AHT)Allied Health Technology">(AHT)Allied Health Technology</option> <option <?php if($_POST[ 'Class']=='(APM)Architecture & Project Management' ){echo 'selected="true"';} ?>value="(APM)Architecture & Project Management">(APM)Architecture & Project Management</option> <option <?php if($_POST[ 'Class']=='(ACR)Automotive Collision Repair' ){echo 'selected="true"';} ?>value="(ACR)Automotive Collision Repair">(ACR)Automotive Collision Repair</option> <option <?php if($_POST[ 'Class']=='(AUT)Automotive Technology' ){echo 'selected="true"';} ?>value="(AUT)Automotive Technology">(AUT)Automotive Technology</option> <option <?php if($_POST[ 'Class']=='(BMT)Business Management Tech' ){echo 'selected="true"';} ?>value="(BMT)Business Management Tech">(BMT)Business Management Tech</option> <option <?php if($_POST[ 'Class']=='(CNT)Computer Networking' ){echo 'selected="true"';} ?>value="(CNT)Computer Networking">(CNT)Computer Networking</option> <option <?php if($_POST[ 'Class']=='(CON)Construction ' ){echo 'selected="true"';} ?>value="(CON)Construction">(CON)Construction</option> <option <?php if($_POST[ 'Class']=='(COS)Cosmetology' ){echo 'selected="true"';} ?>value="(COS)Cosmetology">(COS)Cosmetology</option> <option <?php if($_POST[ 'Class']=='(CUL)Culinary Arts' ){echo 'selected="true"';} ?>value="(CUL)Culinary Arts">(CUL)Culinary Arts</option> <option <?php if($_POST[ 'Class']=='(EEP)Electrical Engineering Prep' ){echo 'selected="true"';} ?>value="(EEP)Electrical Engineering Prep">(EEP)Electrical Engineering Prep</option> <option <?php if($_POST[ 'Class']=='(EMS)Emergency Medical Services' ){echo 'selected="true"';} ?>value="(EMS)Emergency Medical Services">(EMS)Emergency Medical Services</option> <option <?php if($_POST[ 'Class']=='(HVC)Heating, Ventilation & Air Conditioning' ){echo 'selected="true"';} ?>value="(HVC)Heating, Ventilation & Air Conditioning">(HVC)Heating, Ventilation & Air Conditioning</option> <option <?php if($_POST[ 'Class']=='(MES)Maintenance and Environmental Services' ){echo 'selected="true"';} ?>value="(MES)Maintenance and Environmental Services">(MES)Maintenance and Environmental Services</option> <option <?php if($_POST[ 'Class']=='(ISS)Information Support & Services' ){echo 'selected="true"';} ?>value="(ISS)Infractionormation Support & Services">(ISS)Infractionormation Support & Services</option> <option <?php if($_POST[ 'Class']=='(IMT)Interactive Multimedia Technology' ){echo 'selected="true"';} ?>value='(IMT)Interactive Multimedia Technology'>(IMT)Interactive Multimedia Technology</option> <option <?php if($_POST[ 'Class']=='(IPD)Internet Programming & Development' ){echo 'selected="true"';} ?>value="(IPD)Internet Programming & Development">(IPD)Internet Programming & Development</option> <option <?php if($_POST[ 'Class']=='(LND)Landscape Horticulture' ){echo 'selected="true"';} ?>value="(LND)Landscape Horticulture">(LND)Landscape Horticulture</option> <option <?php if($_POST[ 'Class']=='(PCT)Patient Care Technician' ){echo 'selected="true"';} ?>value="(PCT)Patient Care Technician">(PCT)Patient Care Technician</option> <option <?php if($_POST[ 'Class']=='(SMD)Sports Medicine' ){echo 'selected="true"';} ?>value="(SMD)Sports Medicine">(SMD)Sports Medicine</option> <option <?php if($_POST[ 'Class']=='(TPP)Teaching Professions Pathway' ){echo 'selected="true"';} ?>value="(TPP)Teaching Professions Pathway">(TPP)Teaching Professions Pathway</option> <option <?php if($_POST[ 'Class']=='(WLD)Welding' ){echo 'selected="true"';} ?>value="(WLD)Welding">(WLD)Welding</option> </select> <div class="row"> <span class="err"><?php echo $Err4; ?></span> <div style="margin-left: 35%"> <label>Junior</label> <input type="radio" name="Year" <?php if (isset($_POST[ 'Year']) && $_POST[ 'Year']=="Junior" ) echo "checked";?>value="Junior"> <label>Senior</label> <input type="radio" name="Year" <?php if (isset($_POST[ 'Year']) && $_POST[ 'Year']=="Senior" ) echo "checked";?>value="Senior"> </div> </div> <div class="row"> <span class="err"><?php echo $Err2; ?></span> <span>Location: <input type="text" name="Location" size="16" placeholder="Location of Incident" value="<?php echo htmlspecialchars($_POST['Location']);?>"/></span> </div> </div> <div class="row"> <span class="err"><?php echo $Err5; ?></span> <select name="Associate_School" value="<?php echo htmlspecialchars($_POST['Associate_School']); ?>"> <option value="0"><span class="fieldSelect"> Associate School </span> </option> <option <?php if($_POST[ 'Associate_School']=='BERKSHIRE' ){echo 'selected="true"';} ?>value="BERKSHIRE">BERKSHIRE</option> <option <?php if($_POST[ 'Associate_School']=='CARDINAL' ){echo 'selected="true"';} ?>value="CARDINAL")>CARDINAL</option> <option <?php if($_POST[ 'Associate_School']=='CHARDON' ){echo 'selected="true"';} ?>value="CHARDON">CHARDON</option> <option <?php if($_POST[ 'Associate_School']=='FAIRPORT' ){echo 'selected="true"';} ?>value="FAIRPORT">FAIRPORT</option> <option <?php if($_POST[ 'Associate_School']=='HARVEY' ){echo 'selected="true"';} ?>value="HARVEY">HARVEY</option> <option <?php if($_POST[ 'Associate_School']=='KENSTON' ){echo 'selected="true"';} ?>value="KENSTON">KENSTON</option> <option <?php if($_POST[ 'Associate_School']=='KIRTLAND' ){echo 'selected="true"';} ?>value="KIRTLAND">KIRTLAND</option> <option <?php if($_POST[ 'Associate_School']=='MADISON' ){echo 'selected="true"';} ?>value="MADISON">MADISON</option> <option <?php if($_POST[ 'Associate_School']=='NEWBURY' ){echo 'selected="true"';} ?>value="NEWBURY">NEWBURY</option> <option <?php if($_POST[ 'Associate_School']=='PERRY' ){echo 'selected="true"';} ?>value="PERRY">PERRY</option> <option <?php if($_POST[ 'Associate_School']=='RIVERSIDE' ){echo 'selected="true"';} ?>value="RIVERSIDE">RIVERSIDE</option> </select> </div> <div style="margin: 5% 0 5% 0" class="row"> <span class="err"><?php echo $Err6; ?></span> <div style="width: 50%;float:left;"> <input type="checkbox" name="Infraction" value="Disorderly Conduct"><span>Disorderly Conduct</span> <br> <input type="checkbox" name="Infraction" value="Drug/Alcohol"><span>Drug/Alcohol</span> <br> <input type="checkbox" name="Infraction" value="Disruption of Educational Process"><span>Extortion</span> <br> <input type="checkbox" name="Infraction" value="Fighting/Altercation"><span>Fighting/Altercation</span> <br> <input type="checkbox" name="Infraction" value="Fire/Arson"><span>Fire/Arson</span> <br> <input type="checkbox" name="Infraction" value="Forgery/Falsifying"><span>Forgery/Falsifying</span> <br> <input type="checkbox" name="Infraction" value="Gambling"><span>Gambling</span> <br> <input type="checkbox" name="Infraction" value="Harassment"><span>Harassment</span> <br> <input type="checkbox" name="Infraction" value="Hazing"><span>Hazing</span> <br> <input type="checkbox" name="Infraction" value="Panic Inducing Behavior"><span>Panic Inducing Behavior</span> </div> <div style="width: 50%;float:left;"> <input type="checkbox" name="Infraction" value="Pornography"><span>Pornography</span> <br> <input type="checkbox" name="Infraction" value="Profanity/Obscene"><span>Profanity/Obscene</span> <br> <input type="checkbox" name="Infraction" value="Refuseing to Accept discipline"><span>Refuseing to Accept discipline</span> <br> <input type="checkbox" name="Infraction" value="Public Display of Affection"><span>Public Display of Affection</span> <br> <input type="checkbox" name="Infraction" value="Smoking/Tobacco"><span>Smoking/Tobacco</span> <br> <input type="checkbox" name="Infraction" value="Theft"><span>Theft</span> <br> <input type="checkbox" name="Infraction" value="Trespassing"><span>Trespassing</span> <br> <input type="checkbox" name="Infraction" value="Unauthorized Sale or Distribution"><span>Unauthorized Sale or Distribution</span> <br> <input type="checkbox" name="Infraction" value="Vandalism"><span>Vandalism</span> <br> <input type="checkbox" name="Infraction" value="Violations of Law"><span>Violations of Law</span> <br> <input type="checkbox" name="Infraction" value="Weapons"><span>Weapons</span> <br> </div> </div> <div class="row"> <textarea rows="5" style="margin: 5% 0 1% 0;width:100%;" placeholder="Instructor Comments" name="Comments" value="<?php echo htmlspecialchars($_POST['Comments']); ?>"></textarea> </div> <input type="submit" value="Send" name="sub"> </form> 

If you want an array as result for your POST request, you have to add [] to the html elements name like so:

<input type="checkbox" name="Infraction[]" value="Fighting/Altercation" />

This could be accessd like this

foreach ($_POST['Infraction'] as $item) {
    // ...
}

but I would also recommend to use loops and arrays to structure your layout code better. There are many parts which could replaced with simple loops.

$infractions = array("Disorderly Conduct", "...", ...);
foreach ($infractions as $item) {
    echo '<input type="checkbox" name="Infraction[]" value="' . $item . '"><span>' . $item . '</span>
  <br>';
}

That is easier to read and easier to change, if you want to add something or change the structure of your output

Further to my comments. If you don't have a database to grab data from to populate your options, you can simulate one with manual array(s):

<?php
function fetch_options($key = false)
    {
        $array['AP'][]  =   'Auburn Program';
        $array['AP'][]  =   '(ADM)Advanced Manufacturing';
        $array['AP'][]  =   '(AHT)Allied Health Technology';
        $array['AP'][]  =   '(APM)Architecture & Project Management';
        $array['AP'][]  =   '(ACR)Automotive Collision Repair';
        $array['AP'][]  =   '(AUT)Automotive Technology';
        $array['AP'][]  =   '(BMT)Business Management Tech';
        $array['AP'][]  =   '(CNT)Computer Networking';
        $array['AP'][]  =   '(CON)Construction ';
        $array['AP'][]  =   '(COS)Cosmetology';
        $array['AP'][]  =   '(CUL)Culinary Arts';
        $array['AP'][]  =   '(EEP)Electrical Engineering Prep';
        $array['AP'][]  =   '(EMS)Emergency Medical Services';
        $array['AP'][]  =   '(HVC)Heating, Ventilation & Air Conditioning';
        $array['AP'][]  =   '(MES)Maintenance and Environmental Services';
        $array['AP'][]  =   '(ISS)Information Support & Services';
        $array['AP'][]  =   '(IMT)Interactive Multimedia Technology';
        $array['AP'][]  =   '(IPD)Internet Programming & Development';
        $array['AP'][]  =   '(LND)Landscape Horticulture';
        $array['AP'][]  =   '(PCT)Patient Care Technician';
        $array['AP'][]  =   '(SMD)Sports Medicine';
        $array['AP'][]  =   '(TPP)Teaching Professions Pathway';
        $array['AP'][]  =   '(WLD)Welding';

        $array['AS'][]  =   'Associate School';
        $array['AS'][]  =   'BERKSHIRE';
        $array['AS'][]  =   'CARDINAL';
        $array['AS'][]  =   'CHARDON';
        $array['AS'][]  =   'FAIRPORT';
        $array['AS'][]  =   'HARVEY';
        $array['AS'][]  =   'KENSTON';
        $array['AS'][]  =   'KIRTLAND';
        $array['AS'][]  =   'MADISON';
        $array['AS'][]  =   'NEWBURY';
        $array['AS'][]  =   'PERRY';
        $array['AS'][]  =   'RIVERSIDE';

        $array['IN'][]  =   'Disorderly Conduct';
        $array['IN'][]  =   'Drug/Alcohol';
        $array['IN'][]  =   'Disruption of Educational Process';
        $array['IN'][]  =   'Fighting/Altercation';
        $array['IN'][]  =   'Fire/Arson';
        $array['IN'][]  =   'Forgery/Falsifying';
        $array['IN'][]  =   'Gambling';
        $array['IN'][]  =   'Harassment';
        $array['IN'][]  =   'Hazing';
        $array['IN'][]  =   'Panic Inducing Behavior';
        $array['IN'][]  =   'Pornography';
        $array['IN'][]  =   'Profanity/Obscene';
        $array['IN'][]  =   'Refuseing to Accept discipline';
        $array['IN'][]  =   'Public Display of Affection';
        $array['IN'][]  =   'Smoking/Tobacco';
        $array['IN'][]  =   'Theft';
        $array['IN'][]  =   'Trespassing';
        $array['IN'][]  =   'Unauthorized Sale or Distribution';
        $array['IN'][]  =   'Vandalism';
        $array['IN'][]  =   'Violations of Law';
        $array['IN'][]  =   'Weapons';

        return (isset($array[$key]))? $array[$key] : $array;
    }

// This will build your drop downs
function make_options($settings = false)
    {
        $kind       =   (!empty($settings['kind']))? $settings['kind'] : 'AP';
        $def        =   (!empty($settings['def']))? $settings['def'] : 0;

        $options    =   fetch_options($kind);
        $selVal     =   array_search($def,$options);

        ob_start();
        foreach($options as $key => $value) {
?>
        <option<?php if($key == $selVal) echo ' selected="selected"'; ?> value="<?php echo $value; ?>"><?php echo $value; ?></option>
<?php       }
        $data   =   ob_get_contents();
        ob_end_clean();
        return $data;
    }

// This will build your check boxes
function make_checkbox($settings = false)
    {
        $kind       =   (!empty($settings['kind']))? $settings['kind'] : 'IN';
        //$def      =   (!empty($settings['def']))? $settings['def'] : 0;
        $def        =   (!empty($settings['cols']))? $settings['cols'] : 2; 

        $options    =   fetch_options($kind);
        $selVal     =   array_search($def,$options);
        $cols       =   ceil(count($options)/$def);
        $perc       =   100/$def;
        ob_start();

        $i = 1;
        foreach($options as $key => $value) {
                if($i == 1) 
                    echo PHP_EOL.'<div style="width: '.$perc.'%; display: table-cell; vertical-align: top;">'.PHP_EOL;
?>
        <label style="display: inline-block; float: left; width: 100%;">
            <input type="checkbox"<?php if($key == $selVal) echo ' selected="selected"'; ?> name="infraction[]" value="<?php echo $value; ?>" /><span><?php echo $value; ?></span>
        </label>
<?php           if($i == $cols || $i == count($options)) {
                        echo PHP_EOL.'</div>'.PHP_EOL;
                        $i = 0;
                    }
                $i++;
            }
        $data   =   ob_get_contents();
        ob_end_clean();
        return $data;
    }

    // Establish defaults for your options
    $default['Class']               =   (!isset($_POST['Class']))? 0 : $_POST['Class'];
    $default['Associate_School']    =   (!isset($_POST['Associate_School']))? 0 : $_POST['Associate_School'];
?>
<legend>Office Form</legend>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
    <div style="text-align: right;" class='row'>
        <label>Date:</label>
        <span><?php echo date("m/d/Y"); ?></span>
        <label>Time:</label>
        <span><?php echo date("h:i:sa"); ?></span>
        <br>
    </div>
    <div class='row'>
        <label>Students Name<span class="err"><?php echo $Err1; ?></span></label>
        <input autofocus type="text" name="Student_Name" placeholder="Enter Student Name" value="<?php echo htmlspecialchars($_POST['Student_Name']); ?>" />
    </div>
    <div class='row'>
        <span class="err"><?php echo $Err3; ?></span>
        <select name="Class">
            <?php echo make_options(array("kind"=>"AP","def"=>$default['Class'])); ?>
        </select>
        <div class="row">
            <span class="err"><?php echo $Err4; ?></span>
            <div style="margin-left: 35%">
                <label>Junior</label>
                <input type="radio" name="Year" <?php if (isset($_POST[ 'Year']) && $_POST[ 'Year']=="Junior" ) echo "checked";?>value="Junior">
                <label>Senior</label>
                <input type="radio" name="Year" <?php if (isset($_POST[ 'Year']) && $_POST[ 'Year']=="Senior" ) echo "checked";?>value="Senior">
            </div>
        </div>
        <div class="row">
            <span class="err"><?php echo $Err2; ?></span>
            <span>Location: <input type="text" name="Location" size="16" placeholder="Location of Incident" value="<?php echo htmlspecialchars($_POST['Location']);?>"/></span>
        </div>
    </div>

    <div class="row">
        <span class="err"><?php echo $Err5; ?></span>
        <select name="Associate_School">
            <?php echo make_options(array("kind"=>"AS","def"=>$default['Associate_School'])); ?>
        </select>
    </div>
    <div style="margin: 5% 0 5% 0" class="row">
        <span class="err"><?php echo $Err6; ?></span>
        <?php echo make_checkbox(array("kind"=>"IN","cols"=>3)); ?> 
    </div>
    <div class="row">
        <textarea rows="5" style="margin: 5% 0 1% 0;width:100%;" placeholder="Instructor Comments" name="Comments" value="<?php echo htmlspecialchars($_POST['Comments']); ?>"></textarea>
    </div>
    <input type="submit" value="Send" name="sub">
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM