1
votes

I'm creating a quote builder contact type of form where the user is able to choose their features, and at the bottom be able to type their name, email and a little message. I am using the base of a simple contact form and using PHPMailer to send the form. I have created a little test page to see if I can get the results I'm looking for before proceeding with the full project.

We are setting the 'value' of the checkbox input as a number so that the quote builder can price everything up so was hoping there would be a simple way of just sending through via email whether a particular checkbox has been checked or not with a name

Here's the HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <div class="contact">

      <?php if(!empty($errors)): ?>
        <div class="panel">
          <ul><li><?php echo implode('</li><li>', $errors); ?></li></ul>
        </div>
      <?php endif; ?>

      <form action="contact.php" method="post">

        <label>
          Your Name *
          <input type="text" name="name" autocomplete="off"<?php echo isset($fields['name']) ? 'value="' . e($fields['name']) . '"' : '' ?>>
        </label>
        <label>
          Your email address *
          <input type="text" name="email" autocomplete="off"<?php echo isset($fields['email']) ? 'value="' . e($fields['email']) . '"' : '' ?>>
        </label>
        <label>
          Example feature
          <input type="checkbox" name="feature" class="option" value="25">
        </label>
        <label>
          Your message *
          <textarea name="message" row="8"<?php echo isset($fields['message']) ? e($fields['message']) : '' ?>></textarea>
        </label>

        <input type="submit" value="Send">

        <p class="muted">* means a required field</p>

      </form>

    </div>
  </body>
</html>
<?php
  unset($_SESSION['errors']);
  unset($_SESSION['fields']);
?>

Here's part of the PHP: Take note where it mentions $m->Body this is where I'll need to echo the checkbox value.

$m = new PHPMailer;

$m->isSMTP();
$m->SMTPAuth = true;

$m->Host = 'hosthere';
$m->Username = '[email protected]';
$m->Password = 'password';
$m->SMTPSecure = 'ssl';
$m->Port = 465;

$m->isHTML();

$m->Subject = 'Contact form submitted';
$m->Body = 'From: ' . $fields['name'] . ' (' . $fields['email'] . ')<p>' . $fields['message'] . '</p> ' . $fields['feature'];

$m->FromName = 'Contact';

$m->AddAddress('[email protected]', 'User Name');

if($m->send()) {
  header('Location: thanks.php');
  die();
} else {
  $errors[] = "Sorry, could not send email. Try again later.";
}
1
Is this php file where you send email contact.php ?Stephan Sutter
I think it should be $_POST['name'] not $fields['name']. Right?Quynh Nguyen
Yeah, it sends from index.php to contact.php.Ryan
@QuỳnhNguyễn it's using $fields = [ 'name' => $_POST['name'], 'email' => $_POST['email'], 'message' => $_POST['message'] ];Ryan
I just haven't included that part in the code snippet aboveRyan

1 Answers

1
votes

Try changing

$m->Body = 'From: ' . $fields['name'] . ' (' . $fields['email'] . ')<p>' . $fields['message'] . '</p> ' . $fields['feature'];  

To

$feature = "";
if(isset($_POST['feature'])){
   $feature = $_POST['feature'];
}
$m->Body = 'From: ' . $fields['name'] . ' (' . $fields['email'] . ')<p>' . $fields['message'] . '</p> ' . $feature;