0

I am fairly new with using PHP. I have a form that I am using to send a contact form form a webpage to an email address.

I am having trouble passing multiple checkboxes styled with a comma. For example, if the user picks checkbox1 and checkbox4, the code will be styled: checkbox1, , , checkbox4. I don't want to display the commas, unless the user picks the checkboxes. Let me know if that makes sense or not.

This is the HTML:

    <code>

<form id="contact_form" class="contact" method="post" action="email_process.php">
            <input class="firstname text" name="firstname" type="text" placeholder="FIRST NAME:" required>
            <input class="lastname text" name="lastname" type="text" placeholder="LAST NAME:" required><br><br>

            <input class="email text" name="email" type="email" placeholder="EMAIL:" required>
            <input class="name text" name="phone" type="tel" placeholder="PHONE NUMBER:" required><br><br>

            <label>Would you like to be contacted by:</label>
            <input name="how" class="emailbtn radio" type="checkbox" value="Email">
            <label>EMAIL</label>
            <input name="how2" class="phonebtn radio" type="checkbox" value="Phone">
            <label>PHONE NUMBER</label><br><br>

            <div class="one">
            <label class="margin">EVENT DATE:</label><br>
            <input name="month" class="month small" type="number" placeholder="MM">
            <input name="day" class="day small" type="number" placeholder="DD">
            <input name="year" class="year small" type="number" placeholder="YYYY">
            </div>
            <div class="one">
            <label class="margin">EVENT LOCATION:</label><br>
            <input name="location" class="location text" type="text">
            </div>

            <label>Services we may assist you with:</label><br><br>
            <div class="two">
            <input name="services" class="chefbtn radio" type="checkbox" value="Personal Chef">
            <label>PERSONAL CHEF</label><br>
            <input name="services2" class="cateringbtn radio" type="checkbox" value="Private Cooking Classes">
            <label>PRIVATE COOKING CLASSES</label>
            </div>
            <div class="two">
            <input name="services3" class="chefbtn radio" type="checkbox" value="Event Catering">
            <label>EVENT CATERING</label><br>
            <input name="services4" class="cateringbtn radio" type="checkbox" value="Resteraunt Consulting">
            <label>RESTERAUNT CONSULTING</label>
            </div>

            <input name="heard" class="heard text" type="text" placeholder="HOW DID YOU HEAR ABOUT US?">
            <textarea name="comments" class="comments" type="text" placeholder="ADDITIONAL COMMENTS:"></textarea>

            <input class="submit" type="image" src="../images/contact/s1_submit_btn.png">
        </form>
    </code>

This is my PHP:

    <code>

<?php

//Prefedined Variables 
$to = "Enter email here";

// Email Subject
$subject = "Contact from your website.";

// This IF condition is for improving security  and Prevent Direct Access to the Mail Script.
if($_POST) {

// Collect POST data from form
$firstname = stripslashes($_POST['firstname']);
$lastname = stripslashes($_POST['lastname']);
$email = stripslashes($_POST['email']);
$how = stripslashes($_POST['how']);
$how2 = stripslashes($_POST['how2']);
$phone = stripslashes($_POST['phone']);
$month = stripslashes($_POST['month']);
$day = stripslashes($_POST['day']);
$year = stripslashes($_POST['year']);
$location = stripslashes($_POST['location']);
$services = stripslashes($_POST['services']);
$services2 = stripslashes($_POST['services2']);
$services3 = stripslashes($_POST['services3']);
$services4 = stripslashes($_POST['services4']);
$heard = stripslashes($_POST['heard']);
$comments = stripslashes($_POST['comments']);

if ( ! empty($how) && ! empty($how2) ) { 
$contact_type = $how.', '.$how2;
} elseif (! empty($how)){
    $contact_type = $how;
} elseif (! empty($how2)){
    $contact_type = $how2;
}

if ( ! empty($services) || ! empty($services2) || ! empty($services3) || ! empty($services4)) { 
$contact_type2 = $services. ', ' .$services2. ', ' .$services3. ', ' .$services4;
}

// Collecting all content in HTML Table
$content='<table width="100%">
<tr><td  align "center"><b>Contact Details</b></td></tr>
<tr><td>First Name:</td><td> '.$firstname.'</td></tr>
<tr><td>Last Name:</td><td> '.$lastname.'</td></tr>
<tr><td>Email:</td><td> '.$email.' </td></tr>
<tr><td>Phone:</td> <td> '.$phone.'</td></tr>
<tr><td>How Should We Contact You?</td> <td> '.$contact_type.'</td></tr>
<tr><td>Event Date:</td><td> '.$month.' / '.$day.' / '.$year.' </td></tr>
<tr><td>Location:</td> <td> '.$location.'</td></tr>
<tr><td>Which Services Are You Interested In?</td> <td> '.$contact_type2.'</td></tr>
<tr><td>How Did You Hear About Us?</td> <td> '.$heard.'</td></tr>
<tr><td>Comments:</td> <td> '.$comments.'</td></tr>
<tr><td>Date:</td> <td> '.date('d/m/Y').'</td></tr>
</table> ';


// Define email variables
$headers = "From:".$email."\r\n";
$headers .= "Reply-to:".$email."\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8';

if (! empty($how) || ! empty($how2)) {
if (! empty($services) || ! empty($services2) || ! empty($services3) || ! empty($services4)) {
if( ! empty($firstname) && ! empty($lastname) && ! empty($email) && ! empty($phone) && ! empty($month) && ! empty($day) && ! empty($year) && ! empty($location) && ! empty($heard) && ! empty($comments) ) {

// Sending Email 
if( mail($to, $subject, $content, $headers) ) {
print "Thank you, I will get back to you shortly!<br>";
return true;
}
else {
print "An error occured and your message could not be sent.";
return false;
}
}
else {
print "An error occured and your message could not be sent.";
return false;
}
}
}
}

    </code>

EDIT:

After some research and the right guidance I came across an answer that helped me solve this. If anyone else is having the same problem feel free to refer to this.

    <?php

//Prefedined Variables 
$to = "Enter email here";

// Email Subject
$subject = "Contact from your website.";

// This IF condition is for improving security  and Prevent Direct Access to the Mail Script.
if($_POST) {

// Collect POST data from form
$firstname = stripslashes($_POST['firstname']);
$lastname = stripslashes($_POST['lastname']);
$email = stripslashes($_POST['email']);
$how = 'None';
    if(isset($_POST['how']) && is_array($_POST['how']) && count($_POST['how']) > 0){
    $selectedHow = implode(', ', $_POST['how']);
    }
$phone = stripslashes($_POST['phone']);
$month = stripslashes($_POST['month']);
$day = stripslashes($_POST['day']);
$year = stripslashes($_POST['year']);
$location = stripslashes($_POST['location']);
$services = 'None';
    if(isset($_POST['services']) && is_array($_POST['services']) && count($_POST['services']) > 0){
    $selectedServices = implode(', ', $_POST['services']);
    }
$heard = stripslashes($_POST['heard']);
$comments = stripslashes($_POST['comments']);

// Collecting all content in HTML Table
$content='<table width="100%">
<tr><td  align "center"><b>Contact Details</b></td></tr>
<tr><td>First Name:</td><td> '.$firstname.'</td></tr>
<tr><td>Last Name:</td><td> '.$lastname.'</td></tr>
<tr><td>Email:</td><td> '.$email.' </td></tr>
<tr><td>Phone:</td> <td> '.$phone.'</td></tr>
<tr><td>How Should We Contact You?</td> <td> '.$selectedHow.'</td></tr>
<tr><td>Event Date:</td><td> '.$month.' / '.$day.' / '.$year.' </td></tr>
<tr><td>Location:</td> <td> '.$location.'</td></tr>
<tr><td>Which Services Are You Interested In?</td> <td> '.$selectedServices.'</td></tr>
<tr><td>How Did You Hear About Us?</td> <td> '.$heard.'</td></tr>
<tr><td>Comments:</td> <td> '.$comments.'</td></tr>
<tr><td>Date:</td> <td> '.date('d/m/Y').'</td></tr>
</table> ';


// Define email variables
$headers = "From:".$email."\r\n";
$headers .= "Reply-to:".$email."\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8';

if( ! empty($firstname) && ! empty($lastname) && ! empty($email) && ! empty($phone)  && ! empty($selectedHow) && ! empty($month) && ! empty($day) && ! empty($year) && ! empty($location) && ! empty($selectedServices) && ! empty($heard) && ! empty($comments) ) {

// Sending Email 
if( mail($to, $subject, $content, $headers) ) {
print "Thank you, I will get back to you shortly!<br>";
return true;
}
else {
print "Please go back and make sure that all fields have been filled out.";
return false;
}
}
else {
print "An error occured and your message could not be sent.";
return false;
}
}


<form id="contact_form" class="contact" method="post" action="email_process.php">
        <input class="firstname text" name="firstname" type="text" placeholder="FIRST NAME:" required>
        <input class="lastname text" name="lastname" type="text" placeholder="LAST NAME:" required><br><br>

        <input class="email text" name="email" type="email" placeholder="EMAIL:" required>
        <input class="name text" name="phone" type="tel" placeholder="PHONE NUMBER:" required><br><br>

        <label>Would you like to be contacted by:</label>
        <input name="how[]" class="emailbtn radio" type="checkbox" value="Email">
        <label>EMAIL</label>
        <input name="how[]" class="phonebtn radio" type="checkbox" value="Phone">
        <label>PHONE NUMBER</label><br><br>

        <div class="one">
        <label class="margin">EVENT DATE:</label><br>
        <input name="month" class="month small" type="number" placeholder="MM">
        <input name="day" class="day small" type="number" placeholder="DD">
        <input name="year" class="year small" type="number" placeholder="YYYY">
        </div>
        <div class="one">
        <label class="margin">EVENT LOCATION:</label><br>
        <input name="location" class="location text" type="text">
        </div>

        <label>Services we may assist you with:</label><br><br>
        <div class="two">
        <input name="services[]" class="chefbtn radio" type="checkbox" value="Personal Chef">
        <label>PERSONAL CHEF</label><br>
        <input name="services[]" class="cateringbtn radio" type="checkbox" value="Private Cooking Classes">
        <label>PRIVATE COOKING CLASSES</label>
        </div>
        <div class="two">
        <input name="services[]" class="chefbtn radio" type="checkbox" value="Event Catering">
        <label>EVENT CATERING</label><br>
        <input name="services[]" class="cateringbtn radio" type="checkbox" value="Resteraunt Consulting">
        <label>RESTERAUNT CONSULTING</label>
        </div>

        <input name="heard" class="heard text" type="text" placeholder="HOW DID YOU HEAR ABOUT US?">
        <textarea name="comments" class="comments" type="text" placeholder="ADDITIONAL COMMENTS:"></textarea>

        <input class="submit" type="image" src="../images/contact/s1_submit_btn.png">
    </form>

2 Answers 2

1

You need to have the name of those checkboxes as an array. Like:

<label>Would you like to be contacted by:</label>
<input name="how[]" class="emailbtn radio" type="checkbox" value="Email">
<label>EMAIL</label>
<input name="how[]" class="phonebtn radio" type="checkbox" value="Phone">
<label>PHONE NUMBER</label><br><br>

Do same for services:

<input name="services[]" class="chefbtn radio" type="checkbox" value="Personal Chef">
<label>PERSONAL CHEF</label><br>
<input name="services[]" class="cateringbtn radio" type="checkbox" value="Private Cooking Classes">
<label>PRIVATE COOKING CLASSES</label>
</div>
<div class="two">
<input name="services[]" class="chefbtn radio" type="checkbox" value="Event Catering">
<label>EVENT CATERING</label><br>
<input name="services[]" class="cateringbtn radio" type="checkbox" value="Resteraunt Consulting">

Then in your php:

$how = stripslashes($_POST['how']); 
// is actually an array and holds its values in a comma separated format
//no need for how2
//The same approach for services: it's also an array now. if you followed the html above 
$services = stripslashes($_POST['services']);

It will also be good if you assign different unique error messages unlike you did along

if( ! empty($firstname) && ! empty($lastname) && ! empty($email) && ! empty($phone)  && ! empty($how) && ! empty($month) && ! empty($day) && ! empty($year) && ! empty($location) && ! empty($services) && ! empty($heard) && ! empty($comments) ) {

// Sending Email 
if( mail($to, $subject, $content, $headers) ) {
print "Thank you, I will get back to you shortly!<br>";
return true;
}
else {
print "An error occured and your message could not be sent.";//here 1
return false;
}
}
else {
print "An error occured and your message could not be sent.";//here 2
return false;
}
}

You can then find out if it's the outer if( ! empty($firstname) && ... or the mail function that is failing

Sign up to request clarification or add additional context in comments.

11 Comments

I followed what you said and now I am getting a 500 error and the fields are not getting transferred at all.
Please see the edit, for the edited code, and let me know if I may have done something wrong.
Check your apache error log for any server-related errors, Since this is most likely a php error.
It posts that there was an error and doesn't send an email. Could it be the way I am checking that the fields are not empty?
What particular error? Server error or your user-defined error?
|
0

try it with php method array filter and implode:

<?php

$services[] = "test";
$services[] = "";
$services[] = "test";
$services[] = "";

$arr = array_filter($services, function($elem){
    return $elem != "";
});

echo implode(", ", $arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.