发送电子邮件按钮不起作用,打开所需的网址或发送电子邮件,而不是同时打开

时间:2013-08-14 22:31:27

标签: php html forms email

我陷入了两个我无法同时发生的功能之间;我有一个电子邮件提交表单,我需要它在点击"提交"之后将用户重定向到感谢页面。按钮。我为您提供了3个html替代品和1个php文件。

当html是这样的时候,电子邮件会被发送,但重定向不起作用。

            <form id="contact_form" action="mailer.php" method="post" enctype="text/plain">
                                    <div class="message" style="display:none"><div id="contact_alert" class="alert"></div></div>
                <label for="name">Name:</label>
                <input type="text" name="name" id="name" class="full-width help" title="Insert your name" />
                <label for="email">Email:</label>
                <input type="email" name="email" id="email" class="full-width help" title="Insert your email" />
                <label for="message">Message:</label>
                <textarea name="message" id="message" cols="30" rows="7" class="full-width help" title="Insert your message"></textarea>
                <input type="submit" value="Submit" class="button" />
            </form>

当我将最后一行更改为此时,它会再次发送电子邮件,但重定向仍然无效:

            <form id="contact_form" action="mailer.php" method="post" enctype="text/plain">
                                    <div class="message" style="display:none"><div id="contact_alert" class="alert"></div></div>
                <label for="name">Name:</label>
                <input type="text" name="name" id="name" class="full-width help" title="Insert your name" />
                <label for="email">Email:</label>
                <input type="email" name="email" id="email" class="full-width help" title="Insert your email" />
                <label for="message">Message:</label>
                <textarea name="message" id="message" cols="30" rows="7" class="full-width help" title="Insert your message"></textarea>
                <a href="thank-you"><input type="submit" value="Submit" class="button" /></a>
            </form>

最后,当我将最后一行更改为此时,重定向有效,但没有发送电子邮件。

            <form id="contact_form" action="mailer.php" method="post" enctype="text/plain">
                                    <div class="message" style="display:none"><div id="contact_alert" class="alert"></div></div>
                <label for="name">Name:</label>
                <input type="text" name="name" id="name" class="full-width help" title="Insert your name" />
                <label for="email">Email:</label>
                <input type="email" name="email" id="email" class="full-width help" title="Insert your email" />
                <label for="message">Message:</label>
                <textarea name="message" id="message" cols="30" rows="7" class="full-width help" title="Insert your message"></textarea>
                <a href="thank-you"><input type="button" value="Submit" class="button" /></a>
            </form>

这是php文件:

<?php
$to = "info@domain.com";
$subject = "Hi Nick, this is ".$_POST['name'];
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];

$headers = 'From: '.$_POST['email'].'' . "\r\n" .
'Reply-To: '.$_POST['email'].'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

$body = $message;

mail($to, $subject, $body, $headers );
header('Location: http://www.domain.com/thank-you') ;
?>

任何帮助都将受到高度赞赏。谢谢!

2 个答案:

答案 0 :(得分:0)

在第三个html块中,重定向有效,因为它是一个简单的超链接,因为<input type=button>定义了一个可点击的按钮(主要用于JavaScript来激活脚本)。 因此,不会提交任何表格,但超链接将起作用。

在第二个html块中,超链接不起作用,因为<input type=submit>将使其成为一个提交按钮,其默认行为是将表单提交到操作文件,因此超链接将不起作用。

我知道这不能回答这个问题,但只是想告诉你原因。

答案 1 :(得分:0)

  

✓EDIT(用于成功提交的实际代码)


HTML表单

<form id="contact_form" action="mailer.php" method="post">
<div class="message" style="display:none"><div id="contact_alert" class="alert"></div></div>
    <label for="name">Name:</label>
    <input type="text" name="name" id="name" class="full-width help" title="Insert your name" />
    <label for="email">Email:</label>
    <input type="email" name="email" id="email" class="full-width help" title="Insert your email" />
    <label for="message">Message:</label>
    <textarea name="message" id="message" cols="30" rows="7" class="full-width help" title="Insert your message"></textarea>
    <input type="submit" value="Submit" class="button" />
</form>

邮件处理程序

<?php

$to = "email@example.com";

$subject = "Hi Nick, this is ".$_POST['name'];
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];

$headers = 'From: '.$_POST['email'].'' . "\r\n" .
'Reply-To: '.$_POST['email'].'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

$body = $message;

mail($to, $subject, $body, $headers );
header('Location: http://www.example.com/thank-you');

// commented out for testing purposes.
// Email was sent and received successfully
// echo "Success";
?>


  

✓原始答案(2013年8月14日)


=&gt;不应在enctype="text/plain"元素中使用<form>,这就是您遇到问题的原因。

使用此重新格式化的表格尝试:

<form id="contact_form" action="mailer.php" method="post">
<div class="message" style="display:none"><div id="contact_alert" class="alert"></div></div>
    <label for="name">Name:</label>
    <input type="text" name="name" id="name" class="full-width help" title="Insert your name" />
    <label for="email">Email:</label>
    <input type="email" name="email" id="email" class="full-width help" title="Insert your email" />
    <label for="message">Message:</label>
    <textarea name="message" id="message" cols="30" rows="7" class="full-width help" title="Insert your message"></textarea>
    <input type="submit" value="Submit" class="button" />
</form>


我取而代之:

<form id="contact_form" action="mailer.php" method="post" enctype="text/plain">
                                                        /--------------------/

用这个:

<form id="contact_form" action="mailer.php" method="post">

这是无效:

<a href="thank-you"><input type="button" value="Submit" class="button" /></a>

有效:

<a href="thank-you.html">Click here</a>

这也是有效的:

<input type="button" value="Submit" class="button" />