0

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

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

我该怎么办?

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

}

?>

我想这一定与上面的 if else 语句有关。我想说'如果成功并且没有错误,然后去 www.whatever.com' 但我该怎么写呢?

4

3 回答 3

0

在您的表单 PHP 页面的末尾,添加这个,为您的 URL 更改:

$success = "success.html";
header("Location: " . $success);
于 2013-09-09T23:13:30.390 回答
0

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

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

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

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

. 确保你使用die();杀死当前页面。

于 2013-09-09T23:22:30.913 回答
0

看到你的编辑后,试试这个:

快速打开说明:您还可以设置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;
}

更多关于header函数可以在PHP.net网站上查看。

于 2013-09-09T23:29:33.897 回答