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

i'm stuck in between the two desired functions which I can't manage to occur simultaneously; I have an email submission form and I need it to redirect the user to a thank you page after hitting the "Submit" button. I'm providing you with 3 html alternatives and 1 php file.

When the html is like this, the email gets sent but the redirection doesn't work.

            <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>

When i change the last line to this, it sends the email again but redirection still doesn't work:

            <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>

And finally when I change the last line to this, the redirection works but no email gets sent.

            <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>

This is the php file:

<?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'].'' . "
" .
'Reply-To: '.$_POST['email'].'' . "
" .
'X-Mailer: PHP/' . phpversion();

$body = $message;

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

Any help will be highly appreciated. Thanks!

In the third html block, the redirection works because it is a simple hyperlink because <input type=button> defines a clickable button (mostly used with a JavaScript to activate a script). Therefore no form will be submitted but the hyperlink will work.

In the second html block, the hyperlink wont work because <input type=submit> will make it a submit button with a default behavior of submitting the form to the action file and so the hyperlink won't work.

I know this doesn't answer the question but just felt like telling you the reasons.

✓ EDIT (actual code used for successful submission)


HTML 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>

Mail handler

<?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'].'' . "
" .
'Reply-To: '.$_POST['email'].'' . "
" .
'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";
?>


✓ Original answer (Aug. 14, 2013)


This => enctype="text/plain" should not be used inside <form> elements, and that is why you were having problems.

Try it with this reformatted 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>


I replaced this:

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

with this:

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

And this is invalid:

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

This is valid:

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

As is this is also valid:

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