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

问题描述 投票:0回答:2

我已经尝试了一些PHP联系人表单教程,但似乎没有一个适合我。 我不确定自己在做什么错。 我在localhost上进行了测试,什么也没有,所以我继续进行托管,以查看是否可行,但仍然没有任何效果。

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

?>
php html forms contact-form
2个回答
2
投票

您的错误来自行

if ($_POST['submit']) {

这是因为您没有为您的提交按钮指定提交名称。 如果您在HTML中修复此行,则应解决以下问题:

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

我建议您在php.ini文件中设置一个错误日志。 这样,您可以自己看到错误,该错误类似于以下内容:

PHP注意:未定义的索引:在第12行的/var/www/pwd/blah/form_process.php中提交


0
投票

如果您在本地主机模式下工作,则需要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;
    }

}


?>

问候

© www.soinside.com 2019 - 2024. All rights reserved.