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

时间:2013-09-09 23:11:52

标签: php forms

我有一个在同一个网址中提交的表单(例如,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;
}

}

?>

我想它必须与top if else语句有关。我想说'如果成功且没有错误则转到www.whatever.com',但我该怎么写呢?

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

也许您只需要更改HTML,

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

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

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

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

答案 2 :(得分:0)

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

快速打开备注:您还可以将header设置为外部网址,例如:

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网站。