【问题标题】:Redirecting form submission to another page issue将表单提交重定向到另一个页面问题
【发布时间】:2017-08-10 00:47:46
【问题描述】:

在我的 Wordpress 网站上,我想在提交表单后将其重定向到如下页面:example.com/user。

:method:POST
:path:/dash/product/edit/

这是表格的开头:

<!--  Submission Form -->
<form method="post" action="" id="wcv-product-edit" class="wcv-form wcv-formvalidator"> 

和表格的结尾:

<?php WCVendors_Pro_Product_Form::form_data( $object_id, $post_status ); ?>
            <?php WCVendors_Pro_Product_Form::save_button( $title ); ?>

我尝试将其放在表单中:

header('Location: http://www.example.com/user');

但这会将表单所在的整个页面重定向到 example.com/user,而无需提交表单。表单位于此路径:/dash/product/edit/。因此,当我请求此路径(表单所在的位置)时,它只会重定向到 example.com/user

表单控制器:

public function process_submit() { 

        if ( ! isset( $_POST[ '_wcv-save_product' ] ) || !wp_verify_nonce( $_POST[ '_wcv-save_product' ], 'wcv-save_product' ) || !is_user_logged_in() ) { 
            return; 
        }

        $can_submit_live        = WC_Vendors::$pv_options->get_option( 'can_submit_live_products' ); 
        $current_post_status    = isset( $_POST[ 'post_status' ] ) ? $_POST[ 'post_status' ] : ''; 
        $can_edit_approved      = WC_Vendors::$pv_options->get_option( 'can_edit_approved_products' ); 
        $trusted_vendor         = ( get_user_meta( get_current_user_id(), '_wcv_trusted_vendor', true ) == 'yes' ) ? true: false;
        $untrusted_vendor       = ( get_user_meta( get_current_user_id(), '_wcv_untrusted_vendor', true ) == 'yes' ) ? true: false;

        if ( $trusted_vendor ) $can_submit_live = true; 
        if ( $untrusted_vendor ) $can_submit_live = false; 


        $text = array( 'notice' => '', 'type' => 'success' );

如何才能在成功提交表单后才将其重定向到example.com/user

【问题讨论】:

    标签: php wordpress forms redirect


    【解决方案1】:

    您需要某种钩子来提供表单的状态并将其绑定到重定向以作用于钩子。就像使重定向成为一个函数一样,当表单正确提交时您调用该函数。有像Gravity From 这样的第三方插件可以为您做到这一点。如果您想要这样的付费服务,这取决于您。

    public function process_submit() { 
    
      if ( ! isset( $_POST[ '_wcv-save_product' ] ) || !wp_verify_nonce( $_POST[ '_wcv-save_product' ], 'wcv-save_product' ) || !is_user_logged_in() ) { 
        return; 
      }
    
      $can_submit_live        = WC_Vendors::$pv_options->get_option( 'can_submit_live_products' ); 
      $current_post_status    = isset( $_POST[ 'post_status' ] ) ? $_POST[ 'post_status' ] : ''; 
      $can_edit_approved      = WC_Vendors::$pv_options->get_option( 'can_edit_approved_products' ); 
      $trusted_vendor         = ( get_user_meta( get_current_user_id(), '_wcv_trusted_vendor', true ) == 'yes' ) ? true: false;
      $untrusted_vendor       = ( get_user_meta( get_current_user_id(), '_wcv_untrusted_vendor', true ) == 'yes' ) ? true: false;
    
      if ( $trusted_vendor ) {
        header('Location: http://www.example.com/user');
      }else {
        # code...
      }
    
    
      $text = array( 'notice' => '', 'type' => 'success' );
    }

    public function process_submit() { 
    
        if ( ! isset( $_POST[ '_wcv-save_product' ] ) || !wp_verify_nonce( $_POST[ '_wcv-save_product' ], 'wcv-save_product' ) || !is_user_logged_in() ) { 
          return; 
        }
    
        $can_submit_live        = WC_Vendors::$pv_options->get_option( 'can_submit_live_products' ); 
        $current_post_status    = isset( $_POST[ 'post_status' ] ) ? $_POST[ 'post_status' ] : ''; 
        $can_edit_approved      = WC_Vendors::$pv_options->get_option( 'can_edit_approved_products' ); 
        $trusted_vendor         = ( get_user_meta( get_current_user_id(), '_wcv_trusted_vendor', true ) == 'yes' ) ? true: false;
        $untrusted_vendor       = ( get_user_meta( get_current_user_id(), '_wcv_untrusted_vendor', true ) == 'yes' ) ? true: false;
    
        if ( $trusted_vendor ) $can_submit_live = true; 
        if ( $untrusted_vendor ) $can_submit_live = false; 
    
        if ($can_submit_live) {
          header('Location: http://www.example.com/user');
        }else {
          # code...
        }
        
        $text = array( 'notice' => '', 'type' => 'success' );
    }

    【讨论】:

    • 我会使用插件,但我必须使用作为插件一部分的表单:/
    • 因为您自己编写表单和验证;你得到验证失败还是成功结果?如果你这样做,你可以将header('Location: http://www.example.com/user'); 绑定到if statement。重定向是无缘无故发生的,因为它是一个独立的功能,并且不会像查询一样中断默认流程
    • 嗨,编辑了我的问题并在提交按钮上添加了带有 if 语句的表单控制器。请检查一下。
    • 好的,您对来自表单的数据做了什么,您是将数据添加到数据库中还是发送电子邮件? $can_submit_live 的目的是什么?
    • 它进入数据库。其目的与批准/未批准的用户有关。如果是认可用户(trusted_vendor),则可以提交表单。
    猜你喜欢
    • 2017-06-26
    • 1970-01-01
    • 2013-06-14
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多