I have the following code to produce a button with a unique ID of the related Wordpress post (there are several of these links);
<div id="<?php echo $post->ID ;?>" class="button buy">Buy Now</div>
When clicked, this needs to run an external PHP file and run a query based on the post id of the link. Ideally, something like this;
<?php $post = get_post($post->ID);?>
<a href="<?php the_field('itunes_link') ;?>">Buy Now</a>
<?php wp_reset_query() ;?>
I need someone to write the AJAX function to get the $post->ID of the clicked button, store as variable and use that to run the query. The returning data will be used within a popup.
I had attempted this myself, but getting the $post->ID as a variable is something I do not know how to do!
Here's my existing AJAX;
jQuery(".buy").on('click', function(){
[login to view URL]({
url: '<?php echo get_stylesheet_directory_uri(); ?>/ajax/[login to view URL]',
success: function (data) {
[login to view URL]("popup").innerHTML = data;
},
error: function (xhr, status, error) {
if ([login to view URL] > 0) [login to view URL]("popup").innerHTML = ('Error: ' + status);
}
});
});
I hope that makes sense. Please ask if you have any questions.