表单提交到不同的 URL,而不是在同一页面中


Form submitting to a different url as opposed to within the same page

我有一个表单在同一个 url 中提交(例如 form.co.uk 是表单所在的位置,成功提交而不是转到具有不同 URL 的确认页面,它会停留在 form.co.uk 并在同一页面上给出"感谢您与我们联系"的确认。

但是我需要更改它,以便在提交时转到不同的 URL(只是一个感谢页面)

我该怎么做?

.html

<div class="width-inner">
    <?if ($c->submitted && count($c->errors) == 0): ?>
    <div class="thankyou-box">
        <h2>Thank you for your enquiry</h2>
        <h3>We'll get back to you as soon as we can.</h3>
    </div>
    <script type="text/javascript">
    _gaq.push(['_trackPageview', "/thanks"]);
    </script>
    <? else: ?>
    <div class="contact-box left" style="width:650px;">
        <form action="form.php" method="post">
            <div class="heading">
                <h2>Contact Us</h2>
                <h3>Get in touch now.</h3>
            </div>
            <? if(count($c->errors) > 0): ?>
            <div class="error">
                There was a problem with your enquiry
            </div>
            <? endif; ?>
            <div class="vspace20"></div>
            <label>Your Name:</label>
            <input type="text" name="name" class="<?= $c- >requestEmpty('name') ? 'invalid' : '' ?>" value="<?= @$_REQUEST['name'] ?>"/>
            <div class="required">Required field</div>
            <div class="clear"></div>
            <label>Email Address:</label>
            <input type="email" name="email"  class="<?= $c- >requestEmpty('email') ? 'invalid' : '' ?>" value="<?= @$_REQUEST['email'] ?>"/>
            <div class="required">Required field</div>
            <div class="clear"></div>
            <hr/>
            <label>Enquiry:</label>
            <textarea name="enquiry"  class="<?= $c->requestEmpty('enquiry') ? 'invalid' : '' ?>">I  would like to contact you.</textarea>
            <div class="clear"></div>
            <hr/>
            <input type="hidden" name="extra" value="<?=  @$_REQUEST['extra'] ?>"/>
            <div class="notice">Your details will not be used for marketing purposes</div> <br>
            <input type="submit" class="btn-submit right" value="Claim  Now" name="submit"/>
            <div class="clear"></div>
        </form>
    </div>
    </div>

处理程序的代码

<?php

class Contact {
var $submitted = false;
var $errors = array();
var $template = "Name: %s'r'nEmail Address: %s'r'nEnquiry: %s";
public function __construct() {
    if (!empty($_REQUEST['submit'])) {
        $this->submitted = true;
    } else {
        return;
    }
    $requiredFields = array(
        'name', 'email'
    );
    foreach($requiredFields as $key) {
        if ($this->requestEmpty($key)) {
            $this->errors[] = $key;
        }
    }
    if (count($this->errors) == 0) {
        $this->_sendForm();
    }
}
private function _sendForm() {
    $body = sprintf($this->template,
            $_REQUEST['name'],
            $_REQUEST['email'],
            $_REQUEST['enquiry'] . "'r'n" . $_REQUEST['extra']);
    $headers = 'From: auto@magnetikmedia.co.uk' . "'r'n" .
                'Reply-To: auto@magnetikmedia.co.uk' . "'r'n" .
                'X-Mailer: PHP/' . phpversion();
    mail("johnfarrell2012@gmail.com", "New contact form", $body, $headers, "-     fauto@magnetikmedia.co.uk");
header("Location: thank_you.php"); 
exit;
}
public function requestEmpty($key) {
    if (empty($_REQUEST['submit'])) {
        return false;
    }
    if (!isset($_REQUEST[$key])) {
        return true;
    }
    if (trim($_REQUEST[$key]) == "") {
        return true;
    }
    return false;
}
}
?>

我想这一定与顶部有关,否则声明即。我想说"如果成功并且没有错误,那就去 www.whatever.com",但我该怎么写呢?

在表单 PHP 页面的末尾,添加以下内容,为您的 URL 进行了更改:

$success = "success.html";
header("Location: " . $success);

也许你只需要改变你的 HTML,

<form action='page.php' method='post'>

或者在同一页面上处理PHP后发送标头,例如

header('LOCATION:page.php'); die();

.确保使用die();来终止当前页面。

看到您的编辑后,请尝试以下操作:

快速打开说明:您还可以将header设置为外部 URL,例如:

header("Location: http://www.example.com/");

并且必须包括http://


邮件后的重定向示例:

mail("johnfarrell2012@gmail.com", "New contact form", $body, $headers, "- fauto@magnetikmedia.co.uk");
header("Location: thank_you.php"); 
exit;
}

要检查邮件是否成功发送:

(我们只是添加一个if/else条件。

if(mail("johnfarrell2012@gmail.com", "New contact form", $body, $headers, "- fauto@magnetikmedia.co.uk"));
header("Location: thank_you.php"); 
exit;
}
else {
echo "Mail not sent";
exit;
}

有关标题功能的更多信息可以在 PHP.net 网站上查看。

  • http://php.net/manual/en/function.header.php