Redirect After AJAX Call in JQuery WP Nounce in AJAX and JSON.parse

Redirect After AJAX Call in JQuery WP Nounce in AJAX and JSON.parse
/*footerpoupsubmit*/
    jQuery('#footerpoupsubmit').click(function() {
        setCookie('popupclosed', 'yes', 1);

        jQuery.ajax({
            type: 'POST',
            url: consumeracquisition.ajaxurl,
            data: {
                action: 'footer_leadcapture',
                email: jQuery('#footerpopupemail').val(),
                security: consumeracquisition.popup_submit_nounce
            },
            success: function(response) {
                jQuery('#subcribe-popup').hide();
                jQuery('#success-id').show();

                console.log(response);
                var responseobj = JSON.parse(response);
                console.log(responseobj);
                console.log(responseobj['redirect']);

                if (responseobj['success'] == true) {
                    window.location.href = responseobj['redirect'];
                }
            },
            error: function(MLHttpRequest, textStatus, errorThrown) {}

        });

    });
    /*end footerpopupsubmit*/
add_action( 'wp_ajax_footer_leadcapture', 'footer_leadcapture_callback' ); 
add_action( 'wp_ajax_nopriv_footer_leadcapture', 'footer_leadcapture_callback' ); 
function footer_leadcapture_callback(){

    $nouncechek = check_ajax_referer( 'popup_submit_nounce','security' , false );

    if(true == $nouncechek && is_email(sanitize_text_field($_POST['email']) )){

        global $wpdb;
        $tablename = $wpdb->prefix . "leads";

        $sql = $wpdb->prepare("INSERT INTO `$tablename` (`email`) values (%s)", sanitize_text_field($_POST['email']) );
        $wpdb->query($sql);
        

        $to      = get_field('white_paper_request_email','option');
        $subject = 'Pop-Up Sign Up';
        $message = '
Email:'.sanitize_text_field($_POST['email']).'
'; $headers = array('Content-Type: text/html; charset=UTF-8'); wp_mail($to,$subject,$message,$headers); $redirect_url = strtolower('https://adrules.consumeracquisition.com/start/free-access/?utm_content=Popup&email='.sanitize_text_field($_POST['email'])) ; echo json_encode(array('success'=>true, 'redirect'=> $redirect_url )); } die(); }
$crea = array(
'siteurl'               => site_url(),
'adminurl'              => admin_url(),
'ajaxurl'               => admin_url( 'admin-ajax.php' ),
'templateurl'           => get_template_directory_uri(),
'popup_submit_nounce'   => wp_create_nonce('popup_submit_nounce')
    );
wp_localize_script( 'custom-js', 'crea', $crea );
wp_enqueue_script( 'custom-js');

screen81

Leave a Reply

Your email address will not be published. Required fields are marked *