Home > Enterprise >  PHP email form not showing input data
PHP email form not showing input data

Time:07-08

So, I am new to PHP and I followed a tutorial to make an email form for my website. I had to alter a few things, and the form is mostly working but for some reason when you submit the form it is only emailing the message and nothing else.

It redirects fine, but the name & email address don't come through on the email after the message is submitted.

My code for the HTML and PHP pages is below. Thanks!

// EMAIL FORM

<form action="index.php" 
              method="post"
              name="EmailForm">
                <div >
                  <div >
                    <div >
                     
                          <label for="name">Full Name</label>
                          <br>
                          <input type="text" name="name"  placeholder="Full name" required="required">
                 
                          <label for="email">Email Address</label>
                          <br>
                          <input type="text" name="email"  placeholder="Email address" required="required">
                 
                          <label for="subject">Subject</label>
                          <br>
                          <input type="text" name="subject"  placeholder="Enter a subject" required="required">
               
                          <label for="message">Message</label>
                          <br>
                          <textarea  id="textarea" name="message" rows="5" placeholder="Your message" required="required"></textarea>

                      <br>

                      <button type="submit" value="submit" >Submit</button>

                      <br><br>

                    </div>
                  </div>
                </div>
            </form>

// INDEX.PHP CODE

<?php

session_start();

$errors = [];
$inputs = [];

$request_method = strtoupper($_SERVER['REQUEST_METHOD']);

if ($request_method === 'GET') {

    // show the message
    if (isset($_SESSION['message'])) {
        $message = $_SESSION['message'];
        unset($_SESSION['message']);
    } elseif (isset($_SESSION['inputs']) && isset($_SESSION['errors'])) {
        $errors = $_SESSION['errors'];
        unset($_SESSION['errors']);
        $inputs = $_SESSION['inputs'];
        unset($_SESSION['inputs']);
    }
} if ($request_method === 'POST') {
    // check the honeypot and validate the field
    require_once __DIR__ . '/inc/post.php';

    if (!$errors) {
        // send an email
        require_once __DIR__ . '/inc/mail.php';
        // set the message
        $_SESSION['Thanks for contacting us, we will be in touch shortly!'];
    } else {
        $_SESSION['errors'] =   $errors;
        $_SESSION['inputs'] =   $inputs;
    }

    header('Location: contact-beta.html', true, 303);
    exit;
}

// MAIL.PHP CODE

<?php

// get email from the config file
$config = require_once __DIR__ . '/../config/app.php';
$recipient_email = $config['mail']['to_email'];

// contact information
$contact_name = $inputs['name'];
$contact_email = $inputs['email'];
$message = $inputs['message'];
$subject = $inputs['subject'];

// Email header
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=utf-8';
$headers[] = "To: $recipient_email";
$headers[] = "From: $contact_email";
$header = implode('\r\n', $headers);

mail($recipient_email, $subject, $message, $header);

// POST.PHP CODE

<?php

// check the honeypot
$honeypot = filter_input(INPUT_POST, 'nickname', FILTER_SANITIZE_SPECIAL_CHARS);
if ($honeypot) {
    header($_SERVER['SERVER_PROTOCOL'] . ' 405 Method Not Allowed');
    exit;
}

// validate name
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
$inputs['name'] = $name;
if (!$name || trim($name) === '') {
    $errors['name'] = 'Please enter your name';
}

// validate email
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
$inputs['email'] = $email;
if ($email) {
    $email = filter_var($email, FILTER_SANITIZE_EMAIL);
    if (!$email) {
        $errors['email'] = 'Please enter a valid email';
    }
} else {
    $errors['email'] = 'Please enter an email';
}

// validate subject
$subject = filter_input(INPUT_POST, 'subject', FILTER_SANITIZE_SPECIAL_CHARS);
$inputs['subject'] = $subject;
if (!$subject || trim($subject) === '') {
    $errors['subject'] = 'Please enter the subject';
}

// validate message
$message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_SPECIAL_CHARS);
$inputs['message'] = $message;
if (!$message || trim($message) === '') {
    $errors['message'] = 'Please enter the message';
}

CodePudding user response:

You are only sending the message field in the email body

mail($recipient_email, $subject, $message, $header);

You need to include the other information

$body = $message . "\n" . $contact_name . "\n" . $contact_email;
mail($recipient_email, $subject, $body, $header);
  • Related