发送到电子邮件的HTML表单

时间:2018-09-22 19:49:09

标签: php html

我的html表单发送到我的电子邮件时遇到问题。

我对其进行了设置,以便可以转到php文件,并且可以正常工作,但是当我将文件名从index.php更改为contact.php时,它不再发送了。

contact.php:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="style/stylesheet.css">
        <script src="script/scripts.js"></script>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
    </head>
    <body>
        <div class="banner">
            <img src="images/banner.jpg">
        </div>
        <div class="navigation">
            <div class="navbar">
                <a href="index.html" class="active">Home</a>
                <a href="news.html">News</a>
                <div class="dropdown">
                    <button class="dropbtn">Parts 
                        <i class="fa fa-caret-down"></i>
                    </button>
                    <div class="dropdown-content">
                        <a href="cases.html">Cases</a>
                        <a href="motherboards.html">Motherboards</a>
                        <a href="processors.html">Processors</a>
                        <a href="graphics.html">Graphics Cards</a>
                        <a href="storage.html">Storage</a>
                        <a href="powersupplies.html">Power Supplies</a>
                        <a href="ram.html">RAM</a>
                        <a href="other.html">Other</a>
                    </div>
                </div>
                <div class="dropdown">
                    <button class="dropbtn">Builds 
                        <i class="fa fa-caret-down"></i>
                    </button>
                    <div class="dropdown-content">
                        <a href="#">Placeholder</a>
                        <a href="#">Placeholder</a>
                        <a href="#">Placeholder</a>
                        <a href="#">Placeholder</a>
                        <a href="#">Placeholder</a>
                        <a href="#">Placeholder</a>
                        <a href="#">Placeholder</a>
                        <a href="#">Placeholder</a>
                    </div>
                </div> 
                <div class="contact" id="navright">
                    <a href="contact.html" style="float:right;">Contact</a>
                </div>
            </div>
        </div>
        <div class="contact_page">
                <form class="contact_form" action="contactform.php" method="post">
                    <label>Full Name:</label><br/>
                    <input type="text" name="name" placeholder="Full Name"><br/>
                    <label>Your Email:</label><br/>
                    <input type="text" name="mail" placeholder="Email"><br/>
                    <label>Subject:</label><br/>
                    <input type="text" name="subject" placeholder="Subject"><br/>
                    <label>Message:</label><br/>
                    <textarea name="message" class="contacttext" placeholder="Message..."></textarea><br/>
                    <button class="submit" type="submit">Submit</button>
                </form>
        </div>
        <div class="footer">
            <div class="footertext">
                <p>Here at Terry's Computers we do not claim to own any of 
                    the products showed on this website.</p>
                <a href="contact.html"><p>Contact Us</p></a>
            </div>
        </div>
    </body>
</html>

contactform.php:

<?php

if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $subject = $_POST['subject'];
    $mailFrom = $_POST['mail'];
    $message = $_POST['message'];

    $mailTo = "terryjtowell@terrytowell.com";
    $headers = "From: ".$mailFrom;
    $txt = "You have received an Email from ".$name.".\n\n".$message;

    mail($mailTo, $subject, $txt, $headers);
    header("Location: contact.php?mailsend");
}

?>

这不是我的网络托管服务商,因为它可以处理此文件。

index.php:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="stylesheet.css">
        <meta charset="UTF-8">
        <title>Email Form</title>
    </head>
    <body>
        <main>
            <p class="header">E-MAIL FORM</p>
            <form class="contact-form" action="contactform.php" method="post">
                <p class="title">Your Name</p>
                <input type="text" name="name" placeholer="Full Name"><br/>
                <p class="title">Your E-Mail</p>
                <input type="text" name="mail" placeholer="Your E-mail"><br/>
                <p class="title">Subject</p>
                <input type="text" name="subject" placeholer="Subject"><br/>
                <p class="title">Message</p>
                <textarea name="message" maxrows="10" placeholder="Message"></textarea><br/>
                <button type="submit" name="submit"><h2>SUBMIT</h2></button><br/>
            </form>
        </main>
    </body>
</html>

1 个答案:

答案 0 :(得分:2)

您的PHP行:

if (isset($_POST['submit'])) {

将永远不会执行,因为您需要具有name提交的元素,但是您拥有:

<button class="submit" type="submit">Submit</button>

在示例中您说的可行。解决方案应该像添加这样的name属性一样简单:

<button class="submit" name="submit" type="submit">Submit</button>