导航到php表单提交上的特定URL

时间:2016-11-07 07:55:53

标签: php html forms

不熟悉PHP,但我需要用它来提交表单。

但是,我希望它在表单提交后导航到页面。

这是我的代码:

<?php

// configure
$from = 'Demo contact form <demo@domain.com>';
$sendTo = 'Demo contact form <demo@domain.com>';
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'email' => 'Email'); // array variable name => Text to appear in email
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';


try
{
$emailText = "You have new message from contact form\n=============================\n";

foreach ($_POST as $key => $value) {

    if (isset($fields[$key])) {
        $emailText .= "$fields[$key]: $value\n";
    }
}

mail($sendTo, $subject, $emailText, "From: " . $from);

$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);

header('Content-Type: application/json');

echo $encoded;
}
else {
echo $responseArray['message'];
}

现在,提交表单会将我带到一个新页面,该页面只显示$ okMes​​sage文本

<form id="form" class="topBefore" method="post" role="form" action="contact.php">

                    <input id="name" name="name" type="text" placeholder="NAME">
                    <input id="email" name="email" type="text" placeholder="E-MAIL">
                <input id="submit" type="submit" value="Send">

1 个答案:

答案 0 :(得分:0)

使用php标题(&#39;位置:somepage.php&#39;);

if(mail($sendTo, $subject, $emailText, "From: " . $from))
{
header('Location: somepage.php');
}

注意:如果邮件被接受传递,则php mail()函数将返回true,但不保证它将被传递。

编辑:使用try catch语句查看我的简单电子邮件功能。与你的答案相比,它可以帮助你。

// configure
$from = 'Demo contact form <demo@domain.com>';
$sendTo = 'Demo contact form <demo@domain.com>';
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'email' => 'Email'); // array variable name => Text to appear in email
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';

//post varables from your form
//$name = $_POST['name'];
$name = 'John'; 
//$email = $_POST['email'];
$email = 'johndoe@blah.com';

//Email header
$header  = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type: text/html;charset=iso-8859-1" . "\r\n";
$header .= "From: $from" . "\r\n";
$header .= "Reply-To: ..." . "\r\n";

$emailText = "You have new message from contact form <br/>   ============================ <br/> Name : $name <br/> Email : $email <br/>";

try
{
    if (mail('demo@domain.com', $subject, $emailText, $header))
    {
        header('Location: somepage.php');
    }
    else
    {
        throw new Exception($errorMessage);
    }
}
catch (Exception $ex)
{
    echo 'Message: ' . $ex->getMessage();
}