Last active
December 27, 2019 04:17
-
-
Save lasida/0c53154e65965cc6266fdad4c6b879aa to your computer and use it in GitHub Desktop.
WordPress : AJAX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).on('click', '.tour-img, .view-tour', function (e) { //onclick button | |
$.ajax({ | |
type : "post", | |
dataType : "json", | |
url : kmdscrpt.ajaxurl, | |
data : { | |
action: "template_tourpackages", //action to Result PHP | |
post_id : $(this).attr('data-id'), // passing post ID | |
security: kmdscrpt.security // passing security | |
}, | |
success: function(response) { //on succes | |
if(response.type == "success") { | |
jQuery("#popup-tour").html(response.like_count); | |
$('.popup-tour-body').css({ "display": "block" }); //display template | |
} | |
else { | |
alert("Tour package could not be added"); | |
} | |
} | |
}); | |
//ajax get template tour packages | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function theme_scripts() | |
wp_enqueue_script( 'komodoluxury-single', THEME_URI . 'assets/js/functions.js', array('jquery-core'), false, true); | |
wp_localize_script( 'komodoluxury-single', 'kmdscrpt', array( | |
'ajaxurl' => admin_url( 'admin-ajax.php' ), //add ajax url | |
'security' => wp_create_nonce( 'kmd_nonce' ), //add security url | |
)); | |
} | |
//Result PHP | |
add_action("wp_ajax_template_tourpackages", "komodoluxury_template_tourpackages"); | |
add_action("wp_ajax_nopriv_template_tourpackages", "komodoluxury_template_tourpackages"); | |
function komodoluxury_template_tourpackages(){ | |
check_ajax_referer( 'kmd_nonce', 'security' ); //check security | |
echo $_REQUEST['post_id']; //retrive post ID | |
//Result Data | |
wp_die(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment