2015-09-09 49 views
0

我已经尝试了几个PHP联系表单教程,但似乎没有为我工作。我不确定我做错了什么。我在localhost中测试了它,但没有任何结果,所以我继续进行并主持了它,看看它是否可行,但什么都没有。PHP表单加载空白页面,什么都没有发生?

HTML

<form class="form" action="form_process.php" method="post" name="contact_form"> 
    <p class="name"> 
    <label for="name">Name</label><br> 
    <input type="text" name="name_first" id="name" placeholder="First" /> 
    <input type="text" name="name_second" id="name" placeholder="Last" /> 

    </p> 

    <p class="email"> 
    <label for="email">Email</label><br> 
    <input type="text" name="email" id="email" placeholder="[email protected]" /> 
    </p> 

    <p class="text"> 
    <label for="email">Comments</label><br> 
    <textarea name="text" placeholder="Write something to us" /></textarea> 
    </p> 

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

form_process.php

<?php 
    $name_first = $_POST['name_first']; 
    $name_second = $_POST['name_second']; 
    $email = $_POST['email']; 
    $text = $_POST['text']; 
    $from = 'From: '; 
    $to = 'EMAIL HERE'; 
    $subject = 'Hello'; 

    $body = "From: $name_first\n $name_second\n E-Mail: $email\n Message:\n $text"; 

    if ($_POST['submit']) { 
     if (mail ($to, $subject, $body, $from)) { 
      header("Location: index.html"); 
      echo '<p>Your message has been sent!</p>'; 
      exit; 
     } else { 
      echo '<p>Something went wrong, go back and try again!</p>'; 
     } 
    } 

?> 
+1

首先,你有一个名为'name'两个字段(他们应该是一个数组,或有不同的名称),但是这不是你的问题的根源。你有任何错误?你不能像使用它一样使用'header',看看[this SO](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error -in-PHP)。 – Qirel

+0

我怀疑你的'mail()'函数有一些错误的参数,特别是第4个。这是一个“标题”,但你并没有真正指定完整的标题。查看“additional_headers(optional)”下的[PHP'mail()'](http://php.net/manual/en/function.mail.php)。 – Qirel

回答

2

你的错误是从线

if ($_POST['submit']) {

这是因为你没有给你的提交按钮的名称提交。如果您在HTML中修复此行,它应该修复此问题:

<input type="submit" name="submit" value="Send" /> 

我建议您在php.ini文件中设置错误日志。这样,你可以看到错误的自己哪个会说类似的东西:

PHP公告:未定义的索引:在提交 /var/www/pwd/blah/form_process.php上线12

+0

钉住它:)))) –

+1

即使这是真的,他原来的帖子中有这个名字:)(你可以看看编辑历史) – Qirel

+0

这解决了这个问题,它现在重定向回我的index.html。但它并没有给我一个信息说它有效。最重要的是它仍然不发送电子邮件。我通过运行电子邮件应该发送的'php -S localhost:8080'使用localhost:8080,对吧? – b4n4n4

0

如果你在localhost模式下工作,你将需要phpmailer。

首先,你需要从这里https://github.com/PHPMailer/PHPMailer/archive/master.zip

下载PHPMailer的那么您的文件夹中粘贴。如果我的编码不明确,你可以检查从

https://github.com/PHPMailer/PHPMailer

<?php 
require 'PHPMailerAutoload.php'; // Your Path 

$mail = new PHPMailer; 

//$mail->SMTPDebug = 3;        // Enable verbose debug output 

$mail->isSMTP();          // Set mailer to use SMTP 
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers 
$mail->SMTPAuth = true;        // Enable SMTP authentication 
$mail->Username = '[email protected]';     // Your mail 
$mail->Password = 'secret';       // Your mail password 
$mail->SMTPSecure = 'tls';       // Enable TLS encryption, `ssl` also accepted 
$mail->Port = 587;  

$mail->From = '[email protected]'; 
$mail->FromName = 'Mailer'; 
$mail->addAddress('[email protected]', 'Joe User');  // Add a recipient 
$mail->addAddress('[email protected]');    // Name is optional 
$mail->addReplyTo('[email protected]', 'Information'); 
$mail->addCC('[email protected]'); 
$mail->addBCC('[email protected]'); 

$mail->addAttachment('/var/tmp/file.tar.gz');   // Add attachments 
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name 
$mail->isHTML(true);         // Set email format to HTML 



$mail->Subject = 'Here is the subject'; 
$mail->Body = 'This is the HTML message body <b>in bold!</b>'; 
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 

//Check Condition 
if(!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
} else { 
    echo 'Message has been sent'; 
} 

方式二。

如果您在在线模式下进行测试(拥有自己的域名和托管),您可以随意复制并粘贴。

不需要phpmailer。

if(isset($_POST['email'])) $email = $_POST['email']; 
else $email = ""; 



function send_mail($myname, $myemail, $contactname, $contactemail, $subject, $message) { 


    $headers = "MIME-Version: 1.0\n"; 
    $headers .= "Content-type: text/html; charset=iso-8859-1\n"; 
    $headers .= "X-Priority: 1\n"; 
    $headers .= "X-MSMail-Priority: High\n"; 
    $headers .= "X-Mailer: php\n"; 
    $headers .= "From: \"".$myname."\" <".$myemail.">\r\n"; 
    return(mail("\"".$contactname."\" <".$contactemail.">", $subject, $message, $headers)); 
} 

if(isset($Submit) && $Submit=="Go") { 

    $emailContent =''; 


    $sent=send_mail($name, "yourmailname.gmail.com", "Fido", $receipientEmail, "Testing", $emailContent); 
    if($sent) { 
     echo $emailContent; 

     header('Location: contact.php'); 
    }else{ 
     echo "Failed"; 
     exit; 
    } 

} 


?> 

问候