联系表单重定向

时间:2018-06-10 14:12:45

标签: php

我需要感谢页面在新标签页中打开。这可能吗?

我是php的新手,不知道从哪里开始。我知道target="_blank"适用于链接,但我不知道它是否适用于此。

<?php
session_start();
/* Set e-mail recipient */
$myemail  = "draw@example.com";
/* Check all form inputs using check_input function */
$subject = 'I would like to enter the example Draw';
$email    = check_input($_POST['email'], "Enter your email");
$fullname = check_input($_POST['fullname'], "Enter your name");
$headers = "From: $email  <" . $email . ">\r\n";
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) show_error("E-mail address not valid");
/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website)) $website = '';
/* Let's prepare the message for the e-mail */
$message = "Hello!

I would like to enter the example Draw.

Full Name: $fullname

Email: $email

End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message, $headers);

$_SESSION['fullname'] = $_POST['fullname'];
/* Redirect visitor to the thank you page */
header('Location:https://www.example.com/pages/thank-you/draw');
/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);

    if ($problem && strlen($data) == 0) show_error($problem);

    return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}

1 个答案:

答案 0 :(得分:0)

PHP无法做到,你需要JavaScript。我只是提供逻辑,而不是修改你的代码。

选项#1

Demo

<!-- Contact button (open popup) -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-contact">
    Contact
</button>

<!-- Modal -->
<div class="modal fade" id="modal-contact" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-body">
                Contact form
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>

                <!-- Send button -->
                <button id="submit" type="button" class="btn btn-primary">Send</button>
            </div>
        </div>
    </div>
</div>

然后,你需要JavaScript

$(function(){

  // Event click, submit or ...
  $('#submit').click(function(){
    // Process (maybe via AJAX)
    // If success, open new tab "thank you page"
    window.open('http://yoursite/thank-you.html', '_blank');
  });

})

选项#2

<!-- Check when submit (but you need to validate before) -->
<?php if(isset($_POST['fullname']){ ?>
<script type="text/javascript">
    window.open('http://yoursite/thank-you.html', '_blank');
</script>
<?php } ?>