如何在Wordpress中重定向到自定义表单的感谢页面(不联系表单7而不是ajax)?



在客户的主题有一个自定义的联系人表单,而不是一个插件,而不是一个流行的ajax表单片段。我一开始看到这样的形式,想要算出来。在它的动作中是一个文件,也就是在主题中。格式

的代码
<form action="<?php echo get_template_directory_uri(); ?>/data.php" method="post" id="form2">
<ul class="clearfix">
<li><input type="text" name="nameUser" class="required" placeholder="Your name"></li>
<li><input type="tel" name="phoneUser" class="required phone" placeholder="Your phone"></li>
</ul>
<div class="btn-container clearfix">
<div class="wrap-btn-right">
<input type="hidden" name="letter_act" value="<?php echo get_field('letter_action', 'option'); ?>">
<button class="btn-gen" type="submit">Submit</button>
</div>
</div>
</form>

data.php文件的代码,其中所有来自表单的数据都在发送

<!DOCTYPE html>
<html>
<head>
<title>New request</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
html {width: 100%;height: 100%;}
</style>
</head>
<body>
<?php
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(E_ALL);
$to = "$_POST[letter_act]";
$from = "noreply@".$_SERVER['SERVER_NAME'];
$subject = '=?utf-8?B?'.base64_encode('New request:').'?=';
$headers .= 'Return-path: <' . $from . ">rn";
$headers .= 'From: request <' . $from . ">rn";
$headers .= 'Content-type: text/plain; charset=utf-8' . "rn";
$headers .= 'Content-Transfer-Encoding: quoted-printable' . "rnrn";
$message = "Name: $_POST[nameUser]nnPhone: $_POST[phoneUser]";
$mail = mail($to, $subject, $message, $headers);
if ($mail) {
echo "<body style='background: url(wp-content/themes/pzk-theme/img/bg-general.jpg) no-repeat center; background-size:cover;'><div style='width:260px;height:275px;margin:100px auto;background: #fff;color:#0F5F6A;text-align:center;padding:40px 30px 0;font-size: 130%;font-family: Arial,sans-serif;box-shadow:0 10px 15px rgba(0,0,0, .5);'><p>The form was successfully sent. Thank you! </p><p><a href='/' style='display:block;padding:10px 25px;margin: 10px 0 0;font-size:80%;background:#ff530e;border-radius:90px;border-bottom: 5px solid #d1450a;color:#fff;text-decoration:none;width:100px;margin:40px auto 0;'>To the home page</a></p></div>";
}
else {
echo "<body style='background: #fff;'><div style='width:260px;height:275px;margin:100px auto;background: #fff;color:#282828;text-align:center;padding:40px 30px 0;border: 1px solid #c0c0c0;border-radius:10px;font-size: 130%;font-family: Arial,sans-serif;box-shadow:0 10px 15px rgba(0,0,0, .5);'><p>Error! Please repeat your attempt later .</p><p><a href='index.html' style='display:block;padding:10px 25px;background:#00b5a0;border-radius:5px;color:#fff;text-decoration:none;text-shadow:1px 1px 1px #000;width:100px;margin:40px auto 0;'>To the home page</a></p></div>";
}
?>
</body>
</html>

我搜索并找到了方法-将此代码粘贴到data.php文档的顶部

<?php header( "refresh:5;url='https://example.com/thank-you/'" );?>

它工作,重定向到所需的url,但在发送后首先显示块从

if ($mail) {
echo "<body style='background: url(wp-content/themes/pzk-theme/img/bg-general.jpg) no-repeat center; background-size:cover;'><div style='width:260px;height:275px;margin:100px auto;background: #fff;color:#0F5F6A;text-align:center;padding:40px 30px 0;font-size: 130%;font-family: Arial,sans-serif;box-shadow:0 10px 15px rgba(0,0,0, .5);'><p>The form was successfully sent. Thank you! </p><p><a href='/' style='display:block;padding:10px 25px;margin: 10px 0 0;font-size:80%;background:#ff530e;border-radius:90px;border-bottom: 5px solid #d1450a;color:#fff;text-decoration:none;width:100px;margin:40px auto 0;'>To the home page</a></p></div>";
}

如果我删除它并替换为

<?php header( "refresh:5;url='https://example.com/thank-you/'" );?>

我看到空白的白色屏幕。

我是第一次接触那种表格。想要学习更多,并把它做对。也许这是另一种正确的方式,从wordpress本身?

由于您没有验证在前端发送的数据,因此您可以在data.php中构建HTML感谢页面(因为您已经将用户重定向到该页面)。只要关闭php,开始构建html的东西…

但是做你想做的最好的方法是使用ajax,所以你可以向你的php文件发出请求,根据响应,你可以显示任何你想要的。

最新更新