Wordpress表单不提交数据

时间:2017-08-25 15:21:25

标签: php jquery wordpress forms .htaccess

我正在使用wordpress网站,我创建了自定义表单,我在其中获取凭证代码,在验证此代码后,将此表单提交到另一个URL,在表单操作中定义,这里是jquery for this work,

<script type="text/javascript">
    $( document ).ready(function() {
        var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
        $( "#submit_btn_redeem" ).click(function(event) {
          event.preventDefault();
            var form = $("#form-voucher");
            var voucher_code = $("#voucher_code").val();
            var btn = $(this);
            if(voucher_code!=""){
                $("#voucher_code").removeClass("error-fld");
                btn.prop('disabled', true);
                btn.attr("disabled","disabled");
                $(".loading").show();
                var action_data = {
                    'action': 'check_voucher_code',
                    'voucher_code': voucher_code
                };

                $.ajax({ 
                    type: 'POST', 
                    url: ajaxurl, 
                    data: action_data, 
                    dataType: 'json',
                    success: function (response) { 
                        $(".loading").hide();
                        btn.prop('disabled',false);
                        btn.removeAttr("disabled");

                        if(response.status==1){
                            $(".response").html(response.message);
                            $(".response").show().delay(2000).hide(0);
                            alert($("#voucher_code").val());
                            //return false;
                            setTimeout(function(){ 
                                document.getElementById("form-voucher").submit(); 
                            }, 1000);


                        }else{
                            $("#voucher_code").addClass("error-fld");
                            $(".response").html(response.message);
                            $(".response").show().delay(3000).hide(0);

                        }
                    }
                });

            }else{
                $("#voucher_code").addClass("error-fld");
            }



        });
    });
</script>

问题是当它在给定的url上提交时,它首先使用POST方法重定向到301,然后再次生成没有参数的GET请求并汇总到该url,我已经在操作URL上转储$ _REQUEST,而不是在这里获取任何数据,我也检查.htaccess和redirect插件,没有这样的url存在重定向,问题是提交表单用post方法重定向,而用GET方法工作正常,任何机构都可以帮忙

1 个答案:

答案 0 :(得分:0)

你需要在你的主题的function.php文件中定义动作功能,它看起来像这样

add_action( 'wp_ajax_nopriv_check_voucher_code', 'functiontohook' );
add_action( 'wp_ajax_check_voucher_code', 'functiontohook' );

之后你需要在你的ajax请求中指定你已经完成的动作。在functiontohook中,您可以获取所有json数据并执行您想要执行的任何操作。如果有什么不清楚,请告诉我。我希望这会帮助你。