联系表格不发送数据

时间:2012-07-04 08:15:14

标签: php html forms

我使用以下内容建立联系页面,向我发送一封包含访问者数据的电子邮件。

            <form method="post" action="submit.php">
            <label for="Name">Name</label>
            <input type="text" name="Name" id="Name" />

            <label for="Email">eMail</label>
            <input type="text" name="Email" id="Email" />



            <input type="submit" name="submit" value="Send" class="submit-button" />
            </form>

和php:

    <?php

    $EmailFrom = "Me";
    $EmailTo = "me@me.com";
    $Subject = "Test";
    $Name = Trim(stripslashes($_POST['Name'])); 
    $Email = Trim(stripslashes($_POST['Email'])); 

    // validation
    $validationOK=true;
    if (!$validationOK) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
      exit;
    }

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

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

    // redirect to success page 
    if ($success){
      print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.html\">";
    }
    else{
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
    }
    ?>

尝试时,我只获取PHP文件准备的电子邮件正文,但访问者没有插入任何信息,如下所示:

名称: 电子邮件:

2 个答案:

答案 0 :(得分:0)

尝试设置电子邮件的标题信息:

$header  = 'MIME-Version: 1.0'."\r\n";
$header .= 'Content-type: text/plain; charset=iso-8859-1'."\r\n";
$header .= 'From: '.$EmailFrom."\r\n";

$success = mail($EmailTo, $Subject, $Body, $header);

答案 1 :(得分:0)

最后我简单地简化了一下..它似乎是一个“区分大小写”的问题,它阻止了表单与php文件通信。

所以这最终有效:

            <form method="post" action="submit.php">
            <label for="Name">Name</label>
            <input type="text" name="Name" id="name" />

            <label for="Email">eMail</label>
            <input type="text" name="Email" id="email" />

            <label for="Message">Message</label>
            <textarea name="Message" rows="10" id="message"></textarea>

            <input type="submit" name="submit" value="Send" class="submit" id="submit"/>
            </form>

和php:

        <?php


    $EmailFrom = "Test";
    $EmailTo = "test@test.com";
    $Subject = "Testing";
    $Name = Trim(stripslashes($_POST['Name'])); 
    $Email = Trim(stripslashes($_POST['Email'])); 
    $Message = Trim(stripslashes($_POST['Message'])); 

    // validation
    $validationOK=true;
    if (!$validationOK) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
      exit;
    }

    // prepare email body text
    $Body = "";
    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "\n";
    $Body .= "eMail: ";
    $Body .= $Email;
    $Body .= "\n";
    $Body .= "Message: ";
    $Body .= $Message;
    $Body .= "\n";

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

    // redirect to success page 
    if ($success){
      print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
    }
    else{
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
    }
    ?>