0

更新:我有一个简单的 PHP 表单,应该将数据提交到电子邮件地址,但它没有发送。它只是发送字段名称。

这是代码:

<?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>

请帮忙

评论:它现在正在发送,但在页面加载时提交了一封空白电子邮件。有谁知道解决这个问题?

4

6 回答 6

5

将您的提交按钮更改为:

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

你的 PHP 应该看起来更像

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.
    }
}
于 2013-08-17T10:37:58.913 回答
1
<?php
if(isset($_POST['submit'])){

   //your sending email php code 
}
?>
于 2013-08-17T10:36:08.193 回答
1

尝试这个

<?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");
}
?>
于 2013-08-19T04:33:58.640 回答
1
<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"); 
} ?>
于 2013-08-17T10:28:26.103 回答
1

功能mail()

使用SMTP 服务器,因此如果您在本地服务器(如 WAMP 或 ZAMP)上使用它,请确保它已完美配置和安装。

如果它仍然无法正常工作,请休息将您的 php 代码放在两者之间:

<?php

if(isset($_POST['submit']))
{
    // ur php code here
}
?>
于 2013-08-17T10:44:49.587 回答
1

由于您的表单发布了一些值,因此您需要发布这些值<input type="submit">name只是一个标识符,如果您将其称为“提交”,它将不起作用。

尝试添加<input type="submit" value="Whatever you want your text to be">而不是<button name="submit" type="submit" class="btn btn-large btn-primary"><a id="go" rel="leanModal" href="#thankyou">Submit</a></button>.

如果您还有其他问题,请进一步解释。你得到什么错误?尝试添加一些echo以查看您的程序停止工作的位置。

于 2013-08-17T10:59:36.770 回答