php联系表单不起作用

时间:2018-03-17 21:43:41

标签: php mysql forms

我没有PHP的任何技能,但我想在我的HTML文件中创建联系表单,我正在使用Web教程创建这些代码:

HTML:

<form method="post" action="mail.php">
  <label for="name">name</label>
  <input type="text" id="fname" name="name">
  <label for="email">Email</label>
  <input type="email" id="lname" name="email">
  <label for="subject">message</label>
  <textarea id="subject" name="message" style="height:200px"></textarea>
  <input type="submit" value="submit">
</form>

mail.php:

<?php include 'database.php';?>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: MyWeb'; 
$to = 'MyEmail'; 
$subject = 'Hello';


$body = "From: $name\n E-Mail: $email\n Message:\n $message";

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

我创建了一个新数据库并编辑了这个文件: database.php中:

<?php

function OpenCon()
{
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db = "myDatabaseName";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: 
%s\n". $conn -> error);
return $conn;
}

function CloseCon($conn)
{
$conn -> close();
}

?>

然后将此文件上传到htaccesss目录。 但现在表格不起作用。 我该怎么办?

2 个答案:

答案 0 :(得分:0)

为什么不修改mail.php文件?

而不是:

<?php include 'database.php'; ?>
<?php

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: MyWeb';
$to = 'MyEmail';
$subject = 'Hello';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

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

尝试:

<?php include 'database.php'; ?>
<?php

if ($_POST['submit']) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: MyWeb';
    $to = 'MyEmail';
    $subject = 'Hello';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if (@mail($to, $subject, $body, $from)) {
        echo '<p>Your message has been sent!</p>';
    } else {
        echo '<p>Something went wrong, go back and try again!</p>';
    }
}
?>

虽然我不明白你得到了什么错误,但如果能够回应错误并发布错误会很有帮助。欢呼声

答案 1 :(得分:0)

到目前为止,尚不清楚您的问题是否存在于php邮件程序的设置,页面的php代码,页面编码或其他地方。因此,为了找出问题的根源,让我们简化/改变策略并从这开始:

  • 创建两个新的php页面( handlers.php testmail.php )并确保它们是 “UTF-8” 编码。
  • 将以下代码复制到其中。
  • 将自定义电子邮件地址替换为您自己的电子邮件地址在代码中找到 “@ todo” 并采取相应行动。
  • testmail.php 运行。

向我们提供反馈,提供完整的错误消息(如果有)。

handlers.php

<?php

function errorHandler($errno, $errstr, $errfile, $errline) {
    echo '<pre>' . print_r('Error ' . $errno . ' - ' . $errstr . ' in file ' . $errfile . ' on line ' . $errline, TRUE) . '</pre>';    
    exit();
}

function exceptionHandler($exception) {
    echo '<pre>' . print_r('Exception ' . $exception->getCode() . ' - ' . $exception->getMessage() . ' in file ' . $exception->getFile() . ' on line ' . $exception->getLine(), TRUE) . '</pre>';
    exit();
}

set_error_handler('errorHandler');
set_exception_handler('exceptionHandler');

testmail.php

<?php

require 'handlers.php';

error_reporting(E_ALL);
ini_set('display_errors', 1);

$to = 'your-email-here'; /* @todo Replace with a corresponding email address. */
$subject = 'Test email';
$body = 'Hello. This is a test email.';
$headers = 'From: an-email-here'; /* @todo Replace with a corresponding email address. */

$sent = mail($to, $subject, $body, $headers);

if ($sent) {
    echo '<pre>' . print_r('The email was successfully sent.', TRUE) . '</pre>';
} else {
    echo '<pre>' . print_r('An error occurred during your request. Please try again.', TRUE) . '</pre>';
}