简体   繁体   中英

PHP form not submitting entries to email. I'm missing something

Updated: I have a simple PHP form that should submit data to an email address but it isn't sending it. it's just sending the field names.

Here's the code:

<?php 
$ToEmail = 'email@yahoo.com'; 
$EmailSubject = 'Site contact form'; 
$mailheader = "From: ".$_POST["email"]."\r\n"; 
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
$mailheader .= "Content-type: text/plain; charset=iso-8859-1\r\n"; 
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n"; 
$MESSAGE_BODY .= "Surname: ".$_POST["surname"]."\r\n"; 
$MESSAGE_BODY .= "Designation: ".$_POST["designation"]."\r\n";
$MESSAGE_BODY .= "Phone: ".nl2br($_POST["phone"])."\r\n"; 
$MESSAGE_BODY .= "Email: ".nl2br($_POST["email"])."\r\n";
$MESSAGE_BODY .= "Opt in: ".nl2br($_POST["send"])."\r\n"; 
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 
?>


<form class="form-signin" name="index" action="index.php" method="post">

<img src="img/coffee.png" width="235" height="227">
<h2 class="form-signin-heading">Enter your information</h2>
    <input name="name" type="text" class="input-block-level" id="name"  placeholder="Name">
    <input type="text" class="input-block-level" name="surname"  id="surname" placeholder="Surname">
    <input type="text" class="input-block-level" name="designation" id="designation" placeholder="Designation">
    <input type="text" class="input-block-level" name="phone" id="phone" placeholder="Cell Number">
    <input type="text" class="input-block-level" name="email" id="email" placeholder="Email">
    <label class="checkbox">
    <input type="checkbox" value="Send-me-helpful-information" name="send" checked>   <p>Send me helpful information</p>
   <input name="submit" type="submit" value="submit" class="btn btn-large btn-primary" id="go" rel="leanModal" href="#thankyou">


    <p>Terms & Conditions Apply. 
<a href="terms.html">Click HERE</a></p>
  </form>

Please help

Comment: It's sending now but submits a blank email on page load. Does anyone know a fix for this?

Change your submit button to this:

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

Your PHP should look more like

if(!empty($_POST)) { // This is to say if the form is submitted.
    // All your PHP post stuff here. The email sending stuff.
    $errors = array();

    // Do this for all your required fields.
    if(isset($_POST['email']) && $_POST['email'] != '') { // If there's a variable set and it isn't empty.
         $mailheader = "From: " . $_POST['email'] . "\r\n";
    } else {
         $errors[] = "No email submitted";
    }

    print_r($_POST); // This is for debugging. Remove it when satisfied.
    echo $MESSAGE_BODY; // Also for debugging. Ensure this is fine.

    if(empty($errors)) {
        if(mail()) { // Your mail function 
            // Successful mail send, either redirect or do whatever your do for successful send
        } else {
            // Mail failure, inform user the sending failed. Handle it as you wish. Errors will be sent out.
        }
    } else {
        print_r($errors); // See what's going error wise.
    }
}
<form name="form1" id="form1" action="" method="post"> // form like this
<input type="submit" name="submit" id="submit" value="submit"> // submit button like this 
</form>
<?php if(isset($_REQUEST['submit']))
{
$ToEmail = 'email@yahoo.com'; 
$EmailSubject = 'Site contact form'; 
$mailheader = "From: ".$_POST["email"]."\r\n"; 
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
$mailheader .= "Content-type: text/plain; charset=iso-8859-1\r\n"; 
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n"; 
$MESSAGE_BODY .= "Surname: ".$_POST["surname"]."\r\n"; 
$MESSAGE_BODY .= "Designation: ".$_POST["designation"]."\r\n";
$MESSAGE_BODY .= "Phone: ".$_POST["phone"]."\r\n"; 
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 
} ?>
<?php
if(isset($_POST['submit'])){

   //your sending email php code 
}
?>

function mail()

uses SMTP Server so make sure it is perfectly configured and installed if you are using it on your local server such as WAMP or ZAMP.

rest if it still not working put your php code in between:

<?php

if(isset($_POST['submit']))
{
    // ur php code here
}
?>

Since your form is posting some values, you need to have an <input type="submit"> to post those. name is just an indentifier, if you will call it "submit", it won't work.

Try adding <input type="submit" value="Whatever you want your text to be"> instead of <button name="submit" type="submit" class="btn btn-large btn-primary"><a id="go" rel="leanModal" href="#thankyou">Submit</a></button> .

If you have another problem, please explain it more. What errors do you get? Try adding some echo to see where your program stops working.

Try this

<?php    
if($_POST['submit']!="")
{
$ToEmail = 'email@yahoo.com'; 
$EmailSubject = 'Site contact form'; 
$mailheader = "From: ".$_POST["email"]."\r\n"; 
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
$mailheader .= "Content-type: text/plain; charset=iso-8859-1\r\n"; 
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n"; 
$MESSAGE_BODY .= "Surname: ".$_POST["surname"]."\r\n"; 
$MESSAGE_BODY .= "Designation: ".$_POST["designation"]."\r\n";
$MESSAGE_BODY .= "Phone: ".$_POST["phone"]."\r\n"; 
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM