Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

How to submit checkboxes?

<div class="row"> <div class="col-sm-6">

                  <?php  $salarysurveys = array('general' => 'General Surveys',
                                              'industry' => 'Industry Surveys',
                                              'local' => 'Local Surveys',
                                              'club' => 'Club / HR Surveys',
                                              'tailormade' => 'Tailormade Surveys');
                ?>


                  <?php foreach ($salarysurveys as $key => $salarysurvey): ?>
                    <?php echo "<p class='label-p'>$salarysurvey</p>" ; ?>
                  <?php endforeach ?>

                    </div>
                <div class="col-sm-6 center-checkbox">

                   <?php foreach ($salarysurveys as $key => $salarysurvey): ?>
                    <?php echo "<input  name='salarysurveys[]' type='checkbox' value='$key'>"; ?>
                  <?php endforeach ?>


                </div>
              </div><!-- end row -->

// Check form page

<?php

// turn off errors error_reporting(E_ALL ^ E_NOTICE);

if ($_SERVER["REQUEST_METHOD"] == "POST") { $companyname = trim($_POST["companyname"]); $contact_jobtitle = trim($_POST["contact_jobtitle"]); $title = trim($_POST["title"]); $fullname = trim($_POST["fullname"]); $address = trim($_POST["address"]); $tel = trim($_POST["tel"]); $email = trim($_POST["email"]); $details = trim($_POST["details"]); // $salarysurveys = $_POST['salarysurveys']; // $hrpolicysurveys = $_POST['hrpolicysurveys'];

// check stage 1 - these inputs can't be empty
if ($companyname == "" OR $contact_jobtitle == "" OR $title == "" OR $fullname == "" OR $address == "" OR $tel == ""  OR $email == "") {
    $error_message = "You must fill your Company, Job Title, Title, fullname, Address, Phone and Email.";
}

if(!empty($_POST['salarysurveys'])){
    foreach($_POST['salarysurveys'] as $selected){
    echo $selected."</br>";
    }
}


// check stage 2
//check that none of the fields has malicious code
if (!isset($error_message)) {
    foreach( $_POST as $value ){
        if( stripos($value,'Content-Type:') !== FALSE ){
            $error_message = "There was a problem with the information you entered.";
        }// end if
    }// end foreach
}// end if


// check stage 3
// check that the spam honeypot field is left blank
if (!isset($error_message) && $_POST["honeypot"] != "") {
    $error_message = "Your form submission has an error.";
}


// TODO: Send email
// include email
require_once("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();

if (!isset($error_message) && !$mail->ValidateAddress($email)){
    $error_message = "You must specify a valid email address.";
}

 if (!isset($error_message)) { // If $error_message is not set, then we know that we did not encounter an error.
    $email_body = "";
    $email_body = $email_body . "<h1>CONTACT DETAILS</h1>";
    $email_body = $email_body . "Company: " . $companyname . "<br>";
    $email_body = $email_body . "Job Title: " . $contact_jobtitle . "<br>";
    $email_body = $email_body . "Title: " . $title . "<br>";
    $email_body = $email_body . "Full Name: " . $fullname . "<br>";
    $email_body = $email_body . "Address: " . $address . "<br>";
    $email_body = $email_body . "Telephone: " . $tel . "<br>";
    $email_body = $email_body . "Email: " . $email . "<br>";
    $email_body = $email_body . "Website: " . $website . "<br><br>";

    $email_body = $email_body . "<h1>SALARY SURVEYS</h1>";  
    $email_body = $email_body .implode(", ", $_POST['salarysurveys'])."<br>";   
    // $email_body = $email_body . "General Surveys: " . $salarysurveys . "<br>";
    // $email_body = $email_body . "Industry Surveys: " . $salarysurveys . "<br>";
    // $email_body = $email_body . "Local Surveys: " . $salarysurveys . "<br>";
    // $email_body = $email_body . "Club HR Surveys: " . $salarysurveys . "<br>";
    // $email_body = $email_body . "Tailormade Surveys: " . $salarysurveys . "<br>";

    $email_body = $email_body . "<h1>HR Policy Surveys</h1>";   
    //$email_body = $email_body .implode(", ", $hrpolicysurveys)."<br>";          
    // $email_body = $email_body . "Company Car: " . $companycar . "<br>";
    // $email_body = $email_body . "Redundancy: " . $redundancy . "<br>";
    // $email_body = $email_body . "Shift and Overtime: " . $shiftandovertime . "<br>";
    // $email_body = $email_body . "Relocation: " . $relocation . "<br>";
    // $email_body = $email_body . "Maternity and Childcar: " . $maternityandchildcar . "<br>";
    // $email_body = $email_body . "UK Benefits Review: " . $ukbenefitsreview . "<br>";

    $email_body = $email_body . "<h1>Other Details</h1>";   
    $email_body = $email_body . "Details: " . $details . "<br>";

    $mail->SetFrom($email, $companyname, $name );
    $address = "kanisornjoe@gmail.com";
    $mail->AddAddress($address, "Contact Form");
    $mail->Subject    = "Contact Form Submission | " . $companyname;
    $mail->MsgHTML($email_body);


    if($mail->Send()) {
        header("Location: contactform.html.php/?status=thanks");
        exit;
    } else {
      $error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
    }  // }end if

  }// end if

}

?>

2 Answers

Is that your complete HTML? I assume you are writing a form using the <form> tag? If so, are you using the POST or GET method? When submitting, is the user redirected to a different page or does the same one re-load?

Hi christian. Yes it is my complete HTML form (ish). i'm using POST when submitting a form to a page called check-contact-form.php. This page is checking whether the inputs is empty or has any malicious input or not. Once the form is submit, it redirect back to the same page and add a string of ?=status