I have a form which prompt for a secret key. When the user inserts the key and click submit, it will search for the key in db. If the key is been found, the file relevant to the key has to be downloaded automatically. I have completed till the validation with db using AJAX and php. Now I want the file to be downloaded automatically. How can I do that? (It's better if I can automate the download in php file)
JQUERY
$('#review-submit').on('click',function(){
var $secret_pin= $('#pin').val();
jQuery.ajax({
type : 'post',
url : ajax_review_plan_object.ajaxurl,
data : {
action : 'loadReviewPlansByAjax',
'search': $secret_pin
},
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
success: function(response){
console.log(response);
response= response.match(/\d{2}$/);
if(response==1){
$('#empty_pin').fadeIn();
$('#invalid_pin').fadeOut();
$('#correct_pin').fadeOut();
$('#empty_table').fadeOut();
}else if(response==-1){
$('#empty_table').fadeIn();
$('#invalid_pin').fadeOut();
$('#correct_pin').fadeOut();
$('#empty_pin').fadeOut();
}else if(response==2){
$('#correct_pin').fadeIn();
$('#invalid_pin').fadeOut();
$('#empty_table').fadeOut();
$('#empty_pin').fadeOut();
}else if(response==3){
$('#invalid_pin').fadeIn();
$('#correct_pin').fadeOut();
$('#empty_table').fadeOut();
$('#empty_pin').fadeOut();
}
}
});
});
php
function loadReviewPlansByAjax(){
$search= $_POST['search'];
$args = array( 'post_type' => 'reviewplans' );
$loop = new WP_Query( $args );
if($loop->have_posts()){
while ( $loop->have_posts() ) : $loop->the_post();
// the_title();
$key = get_field('pin');
if($search== ''){
echo 1;
return;
} else if($key == $search){
echo 2;
return;
} else{
echo 3;
}
endwhile;
}else{
echo -1;
}
}
add_action( 'wp_ajax_loadReviewPlansByAjax', 'loadReviewPlansByAjax' );
add_action( 'wp_ajax_nopriv_loadReviewPlansByAjax', 'loadReviewPlansByAjax' );
function ajax_coming_soon_enqueues() {
wp_enqueue_script( 'review-plans-ajax-script', get_stylesheet_directory_uri() . '/framework/assets/js/review-plan-ajax.js', array( 'jquery' ), '1.0.0', true );
wp_localize_script( 'review-plans-ajax-script', 'ajax_review_plan_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'ajax_coming_soon_enqueues' );