I am appending items (articles) to a content with some originally existing articles, each article has a button for its deleting, editing, opening. I use jquery & ajax. I want these appended articles' buttons to be able to pass each of their values to jquery click function, as well as produce all the logic there (same way as original articles' buttons do). Is it possible to do something that way?
<!DOCTYPE html>
<html>
<head>
<title>Infinite Software Blog</title>
<meta name="viewport" content="width=device-width, scale=1.0">
<script type="text/javascript" src="/blog/jquery/jquery-3.2.0.min.js"></script>
<script>
$(document).ready(function () {
$('.del_btn').click(function () {
var clickBtnValue = $(this).val();
var ajaxurl = '/blog/helpers/delete_post.php',
data = {'action': clickBtnValue};
$.post(ajaxurl, data, function () {
alert("Post was successfully deleted");
});
});
$('.edit_btn').click(function () {
var clickBtnValue = $(this).val();
console.log(clickBtnValue);
var ajaxurl = '/blog/edit_post.php',
data = {'action': clickBtnValue};
$.post(ajaxurl, data, function () {
window.location.assign("/blog/helpers/updatePost.php");
});
});
$('.post_content_btn').click(function () {
var clickBtnValue = $(this).val();
var ajaxurl = '/blog/view/post.php',
data = {'action': clickBtnValue};
$.post(ajaxurl, data, function () {
window.location.assign("/blog/post.php");
});
});
$('.more_posts_btn').click(function () {
var clickBtnValue = $(this).val();
var ajaxurl = '/blog/helpers/show_more_posts.php',
data = {'action': clickBtnValue};
$.post(ajaxurl, data, function (output) {
$(".content").append(output);
});
});
});
</script>
<link rel="stylesheet" href="/blog/styles/style.css" type="text/css"/>
</head>
show_more_post.php
<?php
if (isset($_POST['action'])) {
include($_SERVER['DOCUMENT_ROOT'] . "/blog/model/classes.php");
$post = new posts();
$postsToDisplay = $post->getPostsQty($_POST['action']);
$newRecordsIndex = $_POST['action'];
for ($i = 1; $i <= $postsToDisplay; $i++) {
$record = $post->getPost($newRecordsIndex);
$newRecordsIndex++;
$elementToAdd = '<article class="topContent"><header><h2><a href="#" title="' . $record[1] . '">' .
$record[1] . '</a></h2></header><footer><p class="post-author">Author: ' . $record[2] . '</p>' .
'</footer><content>' . $record[6] . '</content><footer><p class="post-date">Publish date: ' .
$record[3] . '</p>' . '<p class="post-time">Publish time: ' . $record[4] . '</p><form><button type="submit"
title="Delete post"' . ' value="' . $record[0] . '" class="del_btn">Delete post</button><button type="submit"
title="Edit post"' . ' value="' . $record[0] . '" class="edit_btn">Edit post</button><button type="submit"
title="Open post in a new tab" value="' . $record[0] . '" class="post_content_btn">Open Post</button>
</form></footer></article>';
echo($elementToAdd);
}
}