PHP Forms + Submission Problem

Hey all!

I’ve been trying to set up forms on my website but I’ve run into some problems.

  1. When I fill out my info and submit the form – the corresponding email I get is blank.
  2. When I fill out the form – I just want a quick pop up to say “Success” – but instead it takes me to a completely blank page with “success” in the upper left corner.
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];

$EmailTo = "[email protected]";
$Subject = "New Message Received";

// prepare email body text
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";

$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";

$Body .="Subject: ";
$Body .= $subject;
$Body .= "\n";

$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);

// redirect to success page
if ($success){
   echo "success";
}else{
    echo "invalid";
}

?>

You can also go to whatsauce.com itself and see what happens. I’m a total newbie when it comes to PHP. Let me know if you need more details!

You don’t want (though you could if you want to do it a different way) to redirect if the message was sent properly, simply just show a success message to the screen.

Here’s my contact page and I commented where I setup and display the message in the code:
[php]<?php
require_once DIR . ‘/vendor/autoload.php’;
require_once ‘lib/includes/utilities.inc.php’;

use website_project\email\EmailData as Email;
use website_project\email\Contact as SendForm;

$submit = filter_input(INPUT_POST, ‘submit’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

if (isset($submit) && $submit === ‘submit’) {
$first_name = filter_input(INPUT_POST, ‘first_name’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$last_name = filter_input(INPUT_POST, ‘last_name’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$name = $first_name . ’ ’ . $last_name;
$email = filter_input(INPUT_POST, ‘email’, FILTER_SANITIZE_EMAIL);
$message = filter_input(INPUT_POST, ‘comment’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$topic = filter_input(INPUT_POST, ‘reason’, FILTER_SANITIZE_SPECIAL_CHARS);

/* Spambot check this uses a hidden Captcha technique */
$spambot = filter_input(INPUT_POST, 'subject', FILTER_SANITIZE_SPECIAL_CHARS);


$send = new SendForm(new Email($name, $email, $topic, $message, $spambot));
if (!$send->sendMail()) {
    if (!$send->getName()) {
        $nameError = $send->getNameError();
    } else {
        $displayName = $send->getName();
    }

    if (!$send->getEmail()) {

        $emailError = $send->getEmailError();
    } else {
        $displayEmail = $send->getEmail();
    }
    if (!$send->getMessage()) {
        $messageError = $send->getMessageError();
    } else {
        $displayMessage = $send->getMessage();
    }
} else {
    $success = 'Email successfully sent!';  // Set the success message if message was sent correctly:
}

}

require_once ‘lib/includes/header.inc.php’;
?>

Contact Form First Name Last Name Email Address
Leave blank
comment pricing order info
        <label class="textareaLabel" for="message">Message</label>
        <textarea id="message" name="comment" placeholder="Enter message here..."></textarea>
        <!-- Display the success message if email was sent -->
        <?php echo isset($success) ?'<label>' . $success . '</label>': NULL; ?>
        <input type="submit" name="submit" value="submit">
    </fieldset>
</form> 
<article class="span6 bg-color">
    <div class="contact-info-block">
         <h3>Contact Details</h3>
         <ul class="contact-info">
         <li class="phone"><a href="tel:1-734-748-7661">(734) 748-7661</a></li>
        <li class="mail"><a href="mailto:[email protected]">[email protected]</a></li>
        <li class="twitter"><a href="http://twitter.com/intent/tweet?screen_name=strider64">[member=57087]Strider64[/member]</a></li>
        </ul>
    </div>
</article>
<?php require_once 'lib/includes/footer.inc.php'; [/php]

You can see a working example on my website contact page - see signature below.

HTH John

P.S. if you want to redirect to a different page I would look into using sessions, but I try to do the Keep It Simple Stupid (KISS) method whenever I can. ;D
P.P.S. BTW I do use PHPMailer though I don’t show it being used here, for I have it in one of my classes that I use here. I recommend using a third party mailer for it makes life so much simpler when it comes to coding.

Should I be using a phpmailer too?

Right now I just have it set up to send an alert to my regular email. I always see people’s code that has html and php combined – should I put my php into the index or keep it separate?

Sponsor our Newsletter | Privacy Policy | Terms of Service