提交表单后如何添加消息?PHP,Wordpress
我正在尝试制作一个功能,在用户按下“保存更改”后,用户会收到一条消息,说“您的更改已保存”。提交表单时,页面只是刷新,而不是重定向到任何其他页面 我试过几种方法,但似乎都不管用。我擅长HTML,但不擅长PHP。如果你们中有人能帮助我,我会很高兴的 我的个人资料模板代码如下:提交表单后如何添加消息?PHP,Wordpress,php,wordpress,forms,submit,message,Php,Wordpress,Forms,Submit,Message,我正在尝试制作一个功能,在用户按下“保存更改”后,用户会收到一条消息,说“您的更改已保存”。提交表单时,页面只是刷新,而不是重定向到任何其他页面 我试过几种方法,但似乎都不管用。我擅长HTML,但不擅长PHP。如果你们中有人能帮助我,我会很高兴的 我的个人资料模板代码如下: <div class="user-image"> <div class="bordered-image thick-border"> <?php echo get
<div class="user-image">
<div class="bordered-image thick-border">
<?php echo get_avatar(ThemexUser::$data['user']['ID'], 200); ?>
</div>
<div class="user-image-uploader">
<form action="<?php echo themex_url(); ?>" enctype="multipart/form-data" method="POST">
<label for="avatar" class="button"><span class="button-icon upload"></span><?php _e('Skift billede','academy'); ?></label>
<input type="file" class="shifted" id="avatar" name="avatar" />
<input type="hidden" name="user_action" value="update_avatar" />
<input type="hidden" name="nonce" value="<?php echo wp_create_nonce(THEMEX_PREFIX.'nonce'); ?>" />
</form>
</div>
</div>
<div class="user-description">
<form action="<?php echo themex_url(); ?>" class="formatted-form" method="POST">
<div class="message">
<?php ThemexInterface::renderMessages(); ?>
</div>
<div class="sixcol column">
<div class="field-wrapper">
<input type="text" name="first_name" size="30" value="<?php echo ThemexUser::$data['user']['profile']['first_name']; ?>" placeholder="<?php _e('Fornavn','academy'); ?>" />
</div>
</div>
<div class="sixcol column last">
<div class="field-wrapper">
<input type="text" name="last_name" size="30" value="<?php echo ThemexUser::$data['user']['profile']['last_name']; ?>" placeholder="<?php _e('Efternavn','academy'); ?>" />
</div>
</div>
<div class="clear"></div>
<!-- ADRESSE -->
<?php if(!ThemexCore::checkOption('profile_signature')) { ?>
<div class="field-wrapper">
<input type="text" name="signature" value="<?php echo ThemexUser::$data['user']['profile']['signature']; ?>" placeholder="<?php _e('Adresse','academy'); ?>" />
</div>
<?php } ?>
<div class="user-fields">
<?php ThemexForm::renderData('profile', array(), ThemexUser::$data['user']['profile']); ?>
</div>
<?php } ?>
<a href="#" class="button submit-button"><span class="button-icon save"> </span><?php _e('Gem ændringer','academy'); ?></a>
<input type="hidden" name="user_action" value="update_profile" />
<input type="hidden" name="nonce" value="<?php echo wp_create_nonce(THEMEX_PREFIX.'nonce'); ?>" />
</form>
</div>
您正在提交到另一个url。因此,您可以在函数文件中添加如下内容,以便在url附加消息时显示消息。(您可以使用下面的开关来解释多条消息,并进行查找)此外,您还需要将“?msg=yourmessagevariable”添加到表单操作中
add_action('wp_print_scripts', 'notifcation', 100);
function notifcation () {
if($_GET['msg']) {
$message= sanitize_text_field($_GET['msg']);
switch($message) {
case 'reg-business':
$notehead= 'Please register first';
$msg= 'Please register your business before continuing';
break;
case 'nowvalid':
$notehead= "Congratulations";
$msg='You have successfully connected ';
break;
case 'Completed':
$notehead= 'Edited';
$msg='You have successfully edited your advert';
break;
}
?>
<section id="notification" class="notif notif-notice">
<h6 class="notif-title"><?php echo $notehead;?></h6>
<p><?php echo $msg; ?></p>
<div class="notif-controls">
<a href="index.html" class="notif-close">Close</a>
</div>
</section>
<script type="text/javascript">
jQuery(document).ready(function() {
setTimeout(function(){
jQuery('#notification').css('display', 'none');
},3000)
});
jQuery('.notif-close').click(function(e){
e.preventDefault();
jQuery(this).parent().parent().css('display', 'none');
})
</script>
<?php
}
}
add_action('wp_print_scripts','notification',100);
函数通知(){
如果($\u获取['msg']){
$message=sanitize_text_字段($_GET['msg']);
交换机($message){
案例“注册业务”:
$notehead='请先注册';
$msg='请在继续之前注册您的业务';
打破
案例“nowvalid”:
$notehead=“恭喜”;
$msg='You have successful connected';
打破
案件‘已完成’:
$notehead='Edited';
$msg='您已成功编辑您的广告';
打破
}
?>
jQuery(文档).ready(函数(){
setTimeout(函数(){
jQuery('#notification').css('display','none');
},3000)
});
jQuery('.notif close')。单击(函数(e){
e、 预防默认值();
jQuery(this.parent().parent().css('display','none');
})
谢谢你回答David!我不知道如何正确添加“?msg=yourmessagevariable”?它应该是这样的:谢谢你David!我会尝试一下,然后再给你回复。好吧,我把你编写的代码放进了我的function.php中。(完全相同的代码)。我还输入了代码。您是否将代码放在functions.php文件中?您是否为输出创建了模板文件?(这就是require_once所寻找的)如果不只是用echo$message;
替换那一行;
我还注意到在我从上面的文本中删除那一行之后有一个打字错误。接下来要检查的是你的url的格式,它应该像www.yoursite.com/apage?msg='a message“
如果事实上,如果你在任何url之后键入“?msg='message',它应该输出消息。