dimanche 11 décembre 2016

Display checkbox results in an email with php

I have a "check all that apply" field on my form and would like to display all the results in an email but I can only get one result to show based on the code below. I believe it may have something to do with "implode" but I am new to php and get errors when I try to introduce that.

<?php
if(isset($_POST['submit']) && !empty($_POST['submit'])):
    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
        //your site secret key
        $secret = 'XXXX';
        //get verify response data
        $verifyResponse = file_get_contents('http://ift.tt/1vh5G4o'.$secret.'&response='.$_POST['g-recaptcha-response']);
        $responseData = json_decode($verifyResponse);
        if($responseData->success):
            //contact form submission code
            $businessName = !empty($_POST['businessName'])?$_POST['businessName']:'';
            $employees = !empty($_POST['employees'])?$_POST['employees']:'';
            $struggles = !empty($_POST['struggles'])?$_POST['struggles']:'';
            $siteLocation = !empty($_POST['siteLocation'])?$_POST['siteLocation']:'';
            $lookingFor = !empty($_POST['lookingFor'])?$_POST['lookingFor']:'';

            $to = 'test@gmail.com';
            $subject = 'subject';
            $htmlContent = "
                <p><b>Name of Business: </b>".$businessName."</p>
                <p><b>How many Employees: </b>".$employees."</p>
                <p><b>Company Struggles: </b>".$struggles."</p>
                <p><b>On or offsite Training: </b>".$siteLocation."</p>
                <p><b>What are you looking for?: </b>".$lookingFor."</p>
            ";
            // Always set content-type when sending HTML email
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
            // More headers
            $headers .= 'From:'.$name.' <'.$email.'>' . "\r\n";



            //send email
            @mail($to,$subject,$htmlContent,$headers);

            $succMsg = '<p style="color:#33B26E;">Success</p>';
        else:
            $errMsg = '<p style="color:#f00;">Robot verification failed, please try again.</p>';
        endif;
    else:
        $errMsg = '<p style="color:#f00;">Please click on the reCAPTCHA box.</p>';
    endif;
else:
    $errMsg = '';
    $succMsg = '';
endif;
?>
<!DOCTYPE html>
<html lang="en">
    <div id="contact">
        <form method="POST" action="#contact" name="loginform">

            <script src="http://ift.tt/1xQsaAf" async defer></script> 
            <div class="form-group ">
                <div class="red-star">*</div>
                <label for="businessName" class="sr-only">Name of business</label>
                <input id="businessName" name="businessName" class="form-control" placeholder="Name of Business" type="text" required>
            </div>
            <div class="form-group ">
                <div class="red-star">*</div>
                    <label for="employees" class="sr-only">How many employees?</label>
                    <input id="employees" name="employees" class="form-control" placeholder="How many employees?" type="number" required>
            </div>
            <div class="form-group">
                <div class="red-star">*</div>
                <label class="form-label" for="struggles">What are your company struggles?</label>
                <div class="checkbox">
                    <label for="struggles-0">
                    <input type="checkbox" name="struggles" value="Production">Production
                    </label>
                </div>
                <div class="checkbox">
                    <label for="struggles-1">
                        <input type="checkbox" name="struggles" value="Injuries">Injuries
                    </label>
                </div>
                 <div class="checkbox">
                    <label for="struggles-2">
                        <input type="checkbox" name="struggles" value="Absenteeism">Absenteeism
                    </label>
                            </div>
                <div class="checkbox">
                    <label for="struggles-3">
                        <input type="checkbox" name="struggles" value="Team Morale">Team Morale
                    </label>
                </div>                       
            </div>

            <div class="form-group">
                <label class="form-label" for="siteLocation">Training location preference</label>
                <label for="siteLocation-0">
                    <input type="radio" name="siteLocation" id="siteLocation-0" value="On-site" checked="checked">On-site
                </label>
                <label for="siteLocation-1">
                    <input type="radio" name="siteLocation" id="siteLocation-1" value="Off-site">Off-site
                </label>
            </div>
            <div class="form-group">
                <label class="sr-only" for="lookingFor"></label>                    
                    <textarea class="form-control" id="lookingFor" name="lookingFor" placeholder="What are you looking for?"></textarea>
            </div>
            <div class="form-group">
                <div class="g-recaptcha" data-sitekey="XXXX"></div>
            </div>
            <?php echo $errMsg;echo $succMsg;?>
            <div class="form-group ">
                <input class="float-left btn btn-primary btn-lg" value="SUBMIT" name="submit" type="submit">
            </div>
        </form>
    </div>
</html>




Aucun commentaire:

Enregistrer un commentaire