php - 我的第一个联系表单插件中的 success.php 导致找不到页面
问题描述
更新:对不起,感谢您提供的解决方案......不太清楚如何实施,我想我不太清楚......如果我能理解如何实施解决方案,那将是整洁的......
我还成功地设法让代码工作以创建一个新的数据库表并插入测试数据,但忽略了这一点,所以它并不那么复杂......
我真的很想能够...
- 显示一些我可以放入success.php(或其他地方)的内容,当消息成功发送时,该内容会说Message Sent - YAY!然后我希望能够添加现有的 wp 内容 - 您可能感兴趣的一些服务或产品,并在发送消息后将它们显示在相同的成功结果页面上......
也许在发送消息后有更好的方法来重定向用户......
然后....我还需要将表单数据(尚未完成)保存到我创建的新表(通过插件创建表),然后在管理面板中显示所有表单提交记录的表(不是完毕)
我替换了我的 main.php 文件的内容(插件名称根目录中的插件主 php 文件。
<?php /** template info etc... **/ // Find all .php files in the includes dir of my plug in folder. foreach ( glob( plugin_dir_path( __FILE__ ) . "includes/*.php" ) as $file ) { include_once $file; } ?>
除了主要文件(位于插件根目录中)之外,我的所有文件都在 plugins/plugin-name/includes 中并且正在被发现 - 是的
所以我的includes/webform.php 很好地显示了它发送电子邮件的表单,但我无法通过success.php 或error.php 获得此错误或成功消息。
includes/success.php 现在看起来像这样...根据 DK 的建议
<?Php $Errors = implode(' ', $_SESSION['errMsg']); echo $Errors; ?>
包括/webform.php 现在看起来像这样......
<?php function d6s_opp_html_form() { echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">'; //Form action page is the current url. the form is called by a shortcode that will run functions that are written within this plugin's files, they can be in different files in different folders within the plug in because we have told the plug in to load them in the main plug in file.php horray thay is working.. // other form fields removed to shorten this stakoverflow post echo '<p>'; echo 'Your Name* <br />'; echo '<input type="text" name="d6s-opp-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["d6s-opp-name"] ) ? esc_attr( $_POST["d6s-opp-name"] ) : '' ) . '" size="40" placeholder="First & Last name" required />'; // Now using required - is that better than having to check if not empty in success or fail bit???? echo '<p><input type="submit" name="d6s-submitted" value="Send"/></p>'; echo '</form>'; } //Short code function is here and works GREAT
然后在同一个文件的下面,这是我认为 DK 意味着我应该把他的解决方案的第一部分 1
我想这是我错的地方
function deliver_opp_mail() {
// if the submit button is clicked, send the email
if ( isset( $_POST['d6s-submitted'] ) ) {
//sanitise form values so that form data is readable... eg/ if users enter code/script or formatting symbols, it is not missinterpretted as code and is seen as all text.
$name = sanitize_text_field( $_POST["d6s-opp-name"] );
$email = sanitize_email( $_POST["d6s-opp-email"] );
$messagesubject = sanitize_text_field( $_POST["d6s-opp-subject"] );
$messagecontent = esc_textarea( $_POST["d6s-opp-message"] );
$phone = ( $_POST["d6s-opp-phone"] );
// Would like to consider calling form values via global Variables.
$subject = "NEW OPPURTUNITY: $messagesubject";
$d6sdir = plugin_dir_path( __FILE__ );
//Create the Email Body Message using free text and data from the form.
$message = "New Message From: $name \n MESSAGE: $messagecontent \n Return Email: $email \n Return Phone $phone ";
// get the blog administrator's email address, form data is emailed to this email address.
$to = get_option( 'admin_email' );
// Look into setting a to: Email address in WP Admin Console.
$headers = "From: $name <$email>" . "\r\n";
if ( wp_mail( $to, $subject, $message, $headers ) ) {
//maybe this is in the wrong spot, or perhaps this is not the solution I need, but I have tried this in a few different places and can't get it to work..
$Msg = array(
"You have an error",
"Your mail sent succesfully"
);
$_SESSION['errMsg'] = $Msg;
//this take the user to www.mydomain.com/.....wp-content/plugins/my-plugin/includes/success.php - the file is there, but WP theme not found is displayed.
header("Location: $d6sdir/success.php");
exit;
}
}
}
?>
*不想在表单上方显示成功或错误消息...未来的工作流程计划需要在用户点击提交后将用户带到没有表单和其他内容的页面。
*也使用错误报告 - 在显示未找到主题位之前似乎发生了某些事情或闪烁,并且没有报告其他错误......
<?Php error_reporting(E_ALL); ini_set('display_errors', 1);
****************************** 来自初始帖子.... 真的很想学习。第一次发帖,感谢帮助
插件的目的:创建一个插件,我最终可以为我的小型企业构建一个自定义 CRM 工具并学习编码。
*为什么这么不简单...
解决方案
解决方案 1将消息传递给success.php
$Msg = array(
"You have an error",
"Your mail sent succesfully"
);
$_SESSION['errMsg'] = $Msg;
success.php
$Errors = implode(' ', $_SESSION['errMsg']);
echo $Errors;
解决方案 2:
$_SESSION["errMsg"] = "Your mail sent succesfully";
if(isset($_SESSION['errMsg']) AND !empty($_SESSION['errMsg']) ):
echo "<div class='alert danger'>".$_SESSION['errMsg']."</div>";
endif;
未找到解决方案:
您可以使用$_SERVER['DOCUMENT_ROOT'];
查找根D:/wamp/www
然后是页面的完整网址,如下所示:
echo $_SERVER['DOCUMENT_ROOT']."/yourFolder/test.php";
输出 :D:/wamp/www/yourFolder/test.php
注意exit();
:如果你在之后使用会很好header("Location:");
推荐阅读
- laravel - 在 laravel nova 中注册公共 api 路由,无需身份验证
- android - 我应该将 ViewModel 与 UseCase 一起使用吗?
- c# - ASP.NET:当 http 状态为 4xx 时,Webmethos 不返回有效负载
- amazon-web-services - 如何更快地在 AWS 上发布无服务器应用程序?
- python - 捕获 PyQt5 的 QTableWidget 中的行之间失去焦点的事件
- vb.net - 带有单引号错误的 VB.Net SQLite Select 语句
- c# - 令人困惑的 C# 构造函数
- mysql - SQL Distinct count 累计
- python - 熊猫在符合我标准的特定行之前和之后选择特定行
- apache-camel - 如何在 Camel 中引入延迟以防止在文件被复制之前锁定文件?