top of page

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

  • Writer: سُلَيْمَان بْن دَاوُوْد
    سُلَيْمَان بْن دَاوُوْد
  • Aug 16, 2020
  • 1 min read

    /*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 = '<table>
                        <tr><td>Email:</td><td>'.sanitize_text_field($_POST['email']).'</td></tr>
                    </table>';
        $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');



Recent Posts

See All
Google ReCAPTCHA with AJAX

This is how you can pass on AJAX .js var userdata = { 'action' : 'signin_member', 'username' : jQuery('#user_name').val(), 'password' :...

 
 
 

Comments


© 2020 by syednazrulhassan

bottom of page