【问题标题】:How to add message after submitting form? PHP, Wordpress提交表单后如何添加消息? PHP, WordPress
【发布时间】:2014-11-18 07:58:42
【问题描述】:

我尝试制作一个功能,在用户按下“保存更改”后,用户会收到一条消息“您的更改已保存”。当表单被提交时,页面只是被刷新,而不是重定向到任何其他页面。

我尝试了几件事,但似乎没有任何效果。我擅长 HTML 但不擅长 PHP。如果你们中的任何人可以帮助我,我会很高兴!

我的个人资料模板的代码如下:

<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>

【问题讨论】:

    标签: php wordpress forms submit message


    【解决方案1】:

    您正在提交到另一个网址。因此,您可以在函数文件中添加类似下面的内容,以便在 url 附加 msg 时显示消息。 (您可以使用下面的开关来处理多条消息,查看它)您还需要将 `?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
    
        }
    
    
    
    }
    

    【讨论】:

    • 感谢您回答大卫!我不是如何正确添加“?msg = yourmessagevariable”?它应该看起来像这样:
      另外我看到两个地方有一个
      ,我应该在哪个地方添加代码?最后是 notification.php 文件。那应该包含什么?我不确定您所说的“笔记模板”是什么意思。如果我可能会问一些愚蠢的问题,我很抱歉,但我真的是 PHP 新手.. 提前谢谢你!
    • &lt;form action="&lt;?php echo themex_url(); ?&gt;?msg=Completed"&gt; 如果没有 ?已经在那里(我看不到 THEMEX_URL 的值)。您的页面上有两个表单,如果需要,您可以同时使用,第一个看起来像是图像提交和更新头像,所以那里可能有不同的消息?
    • 谢谢大卫!我会尝试出来并回复你。
    • 好的,我把你做的代码放到我的function.php中。 (完全相同的代码)。我还在每个表单中输入了
      。 (我也分别尝试过)但仍然没有成功。每次我尝试新的东西时,我都会通过检查网站来检查它,以确保文本没有隐藏在某个地方。
    • 你把代码放在你的functions.php文件里了吗?另外,您是否为输出创建了模板文件? (这就是 require_once 正在寻找的)如果不只是用echo $message; 替换该行,我还注意到在我从上面的文本中删除的行之后有一个错字。接下来要检查的是你的 url 的格式,它应该像 www.yoursite.com/apage?msg='a message" 如果事实上你在任何 url 之后输入 `?msg='message' 它应该输出消息。
    猜你喜欢
    • 2011-07-26
    • 2015-10-16
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2017-11-17
    • 2014-05-29
    相关资源
    最近更新 更多