WordPress小部件中的rsvp表单:提交两次,为什么?

时间:2014-11-25 12:10:19

标签: javascript php wordpress forms widget

我正在创建一个包含rsvp表单的自定义WordPress窗口小部件。提交时,表单会发送2封电子邮件,一封给组织者,另一封给访客。 我的问题:两封电子邮件都发送两次(两次发送给组织者,两次发送给访客)。我的代码出了什么问题?

代码与此类似:

function widget($args, $instance)
{
    //init my vars here

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $raw_datas = $_POST["widget-my_widget_name"];

        if (!empty($raw_datas)) {

            //this fct treats form data
            $rsvpdata = nd_manage_rsvp($raw_datas);

            if ($rsvpdata['status'] == 'success') {

                //init $to, $subject, $message, $headers according to $rsvpdata
                $sent = wp_mail( $to, $subject, $message, $headers, $attachments );
                $sent2 = wp_mail( $to2, $subject2, $message2, $headers2, $attachments2 );

                if(!($sent === true && $sent2 === true)) {
                    //failure notice here
                    $rsvpdata['status'] = 'failure';
                } else {
                    //reinit my vars here
                    $rsvpdata['status'] = 'sent';
                }
            }
        }
    } ?>

    <div class="description-text">

        <form id="<?php echo $this->get_field_id('rsvp-form'); ?>" name="<?php echo $this->get_field_name('rsvp-form'); ?>" action="#<?php echo $this->id; ?>" method="post" onsubmit="return validate(<?php echo '\'' . $this->id . '\''; ?>)">

            //some form inputs here
            <input class="rsvp-submit mybutton" type="submit" value="<?php _e('Send','textdomain');?>">
        </form> 

    </div>

<?php
}

编辑:我的JS函数“validate”只是验证,没有ajax来处理表单:

//validate rsvp form values
function validate(formid) {

    var attends = jQuery("#widget-"+formid+"-attend_yes");
    var events = jQuery("#widget-"+formid+"-events_check");
    var minOneCB = jQuery('#'+formid+' input:checkbox').is(':checked');

    var CName = jQuery("#widget-"+formid+"-name");
    var CEmail = jQuery("#widget-"+formid+"-email");

    CName.tipsy({trigger: 'manual', title: 'data-tipsy', offset: 1});
    CEmail.tipsy({trigger: 'manual', title: 'data-tipsy', offset: 1});
    events.tipsy({trigger: 'manual', title: 'data-tipsy', offset: 5});

    events.tipsy("hide");
    CName.tipsy("hide");
    CEmail.tipsy("hide");

    jQuery(document).on('click', function(event) {
        if (!jQuery(event.target).closest('.rsvp-submit').length) {
            events.tipsy("hide");
            CName.tipsy("hide");
            CEmail.tipsy("hide");
        }
    });

    if (attends.is(':checked') && !minOneCB){
        events.tipsy("show");
        return false;
    }

    if(CName.val() == ''){
        CName.tipsy("show");
        return false;
    }

    //isEmail is a function that check if email is valid
    if(CEmail.val() == '' || !isEmail(CEmail.val()) ){
        CEmail.tipsy("show");
        return false;
    }

}

0 个答案:

没有答案