"给我发电子邮件"联系表格不起作用

时间:2017-04-20 06:46:17

标签: php email contact-form

我正在为我的网页编写联系表单,但有些东西不起作用,我无法找到错误的内容。当我按下提交按钮时,它只是重新加载网页。如果有人不介意检查我的代码,我会很感激。

<?php 
if(isset($_POST['submit'])){
    $to = "myEmail@gmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $name = $_POST['name'];
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $name . " wrote the following:" . "\n\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to, $subject, $message, $headers);
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
?>


 <section id="contact" class="parallax-section">
  <div class="overlay"></div>
    <div class="container">
        <div class="row">

            <div class="col-md-offset-2 col-md-8 col-sm-offset-1 col-sm-10">
            <div class="wow fadeInUp section-title" data-wow-delay="0.3s">
                <h2>Susisiekite su mumis</h2>
                <h4>mes visada pasiruoše atsakyti į jums rūpimus klausimus</h4>
            </div>
                <div class="contact-form wow fadeInUp" data-wow-delay="0.7s">
                    <form id="contact-form" method="POST" action="#">
                        <input name="name" type="text" class="form-control" placeholder="Vardas, Pavardė" required>
                        <input name="email" type="email" class="form-control" placeholder="Elektroninis paštas" required>
                        <textarea name="message" class="form-control" placeholder="Jūsų žinutė" rows="5" cols="30" required></textarea>
                        <input type="submit" class="form-control submit" name="submit" value="SIŲSTI">
                    </form>
                </div>
            </div>

        </div>
    </div>
</section>

2 个答案:

答案 0 :(得分:0)

你的问题有点不清楚。但是,表单将再次显示,因为您的代码设置为始终显示表单。如果您希望显示一条消息,说明已发送电子邮件,则应在if块中放置一个echo语句。如果您不想显示表单,可以将表单放在else块中。这样的事情应该有效:

<?php 
if(isset($_POST['submit'])){
    $to = "myEmail@gmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $name = $_POST['name'];
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $name . " wrote the following:" . "\n\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    $success = mail($to, $subject, $message, $headers);
    if ($success) {
        echo "Your message has been sent.";
    } else {
        echo "An error was encountered.";
    }
}
else { //begin else section
?>


 <section id="contact" class="parallax-section">
  <div class="overlay"></div>
    <div class="container">
        <div class="row">

            <div class="col-md-offset-2 col-md-8 col-sm-offset-1 col-sm-10">
            <div class="wow fadeInUp section-title" data-wow-delay="0.3s">
                <h2>Susisiekite su mumis</h2>
                <h4>mes visada pasiruoše atsakyti į jums rūpimus klausimus</h4>
            </div>
                <div class="contact-form wow fadeInUp" data-wow-delay="0.7s">
                    <form id="contact-form" method="POST">
                        <input name="name" type="text" class="form-control" placeholder="Vardas, Pavardė" required>
                        <input name="email" type="email" class="form-control" placeholder="Elektroninis paštas" required>
                        <textarea name="message" class="form-control" placeholder="Jūsų žinutė" rows="5" cols="30" required></textarea>
                        <input type="submit" class="form-control submit" name="submit" value="SIŲSTI">
                    </form>
                </div>
            </div>

        </div>
    </div>
</section>

<?php

} //closes else section

答案 1 :(得分:0)

您可以将表单action属性保留为空白:

<form id="contact-form" method="POST" action="">

调试你的PHP代码:

<?php 
    if (isset($_POST['submit'])) {
        $to = "myEmail@gmail.com"; // this is your Email address
        $headers = "From:" . $from;
        $headers2 = "From:" . $to;
        $send_email = mail($to, $subject, $message, $headers);
        echo "Debugging mail send code.... <br/>"; // Add this line
        var_dump($send_email); // Add this line. Check if it's true or false
    }
?>
相关问题