我将表单数据发布到同一页面以在Wordpress中处理它时遇到问题 . 我创建了一个名为[lwCSForm]的短代码,它可以工作并在页面上添加已添加的表单 . 问题是,当我按提交页面时,不会注意到$ _POST ['submit']变量,而我的代码无法处理数据 .
I have tested to use these actions in the form:
-
action="php echo $_SERVER['PHP_SELF']"
(我无法让php标签在这里工作) -
action="php echo $post->post_title"
(我无法让php标签在这里工作) -
甚至直接写页面名称:
action="about"
This is the function to display the form through shortcode on a page:
function DisplayCustomSettingsForm_shortcode() {
global $post;
if( !is_user_logged_in() ){
echo '<div class="lwCSForm-notloggedin">
<p> Please login to view the content! </p>
</div>';
return;
}
if( isset( $_POST['submit'] ) )
{
echo "The submit button is pressed and has data";
/* Code to update customers settings, like avatar, email, username */
} else {
$phpself = $post->post_title; // get the current page
?>
<!-- LwCSForm Plugin-->
<section id="lwCSForm-wrapper">
<form name="lwCSForm" id="lwCSForm" method="post" action="<?php $phpself ?>" autocomplete="on">
Name: <input type="text" name="realname" placeholder="Your Name" >
Sitename: <input type="text" name="username" placeholder="Username" >
E-mail: <input type="text" name="email" placeholder="E-mail" >
Avatar: <input type="text" name="avatar" placeholder="Avatar" >
<input type="submit" >
</form>
</section>
<!-- END LwCSForm Plugin-->
<?php
}
}
add_shortcode( 'lwCSForm', 'DisplayCustomSettingsForm_shortcode');
Picture of the added form with the shortcode
为什么wordpress不会处理这些信息?并再次免责声明 . 我无法在这里打印php标签,但它们在源代码中 .
2 回答
我发现你可以编写如下所示的函数,并且更容易进行表单验证和数据处理 . 请注意
action=""
正在使用Wordpress的"the_permalink()"函数 . 这使我们能够在相同的函数中编写所有已发布数据的验证和处理,而不是将其单独挂钩到Wordpress的'INIT'函数 .将您的代码更改为
我已经将POST处理移动到
init
钩子,并且我已经为您的表单命名,因此我们可以通过$ _POST数组识别它 .