简体   繁体   English

PHP表单加载空白页,什么都没有发生?

[英]PHP form loads blank page and nothing happens?

I've tried out a few PHP contact form tutorials but none seem to work for me. 我已经尝试了一些PHP联系人表单教程,但似乎没有一个适合我。 I'm not sure what I'm doing wrong. 我不确定自己在做什么错。 I tested it in localhost and nothing, so I went ahead and hosted it to see if that would work but still nothing. 我在localhost上进行了测试,什么也没有,所以我继续进行托管,以查看是否可行,但仍然没有任何效果。

HTML 的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="mail@example.com" />
  </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 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>'; 
        }
    }

?>

Your error is from the line 您的错误来自行

if ($_POST['submit']) {

This is because you did not give your submit button a name of submit. 这是因为您没有为您的提交按钮指定提交名称。 If you fix this line in your HTML it should fix the issue: 如果您在HTML中修复此行,则应解决以下问题:

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

I recommend that you set an error log in your php.ini file. 我建议您在php.ini文件中设置一个错误日志。 That way you can see the error for yourself which would have said something similar to: 这样,您可以自己看到错误,该错误类似于以下内容:

PHP Notice: Undefined index: submit in /var/www/pwd/blah/form_process.php on line 12 PHP注意:未定义的索引:在第12行的/var/www/pwd/blah/form_process.php中提交

If you are working in localhost mode you will need phpmailer. 如果您在本地主机模式下工作,则需要phpmailer。

First you need download phpmailer from here https://github.com/PHPMailer/PHPMailer/archive/master.zip 首先,您需要从此处https://github.com/PHPMailer/PHPMailer/archive/master.zip下载phpmailer。

Then paste in your folder. 然后粘贴到您的文件夹中。 If my coding doesn't clear you, you can check from 如果我的编码不能清除您的声音,您可以从

https://github.com/PHPMailer/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 = 'user@example.com';                 // Your mail 
$mail->Password = 'secret';                           // Your mail password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;     

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$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';
}

Second way. 第二种方式。

If you working testing in online mode(Have own domain and hosting), you can just randomly copy and paste. 如果您以在线模式(拥有自己的域和托管)进行测试,则可以随意复制和粘贴。

Doesnt required phpmailer. 不需要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;
    }

}


?>

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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