I have a users table that includes a column name called "roles". I want to select the details of the user where the column name "roles" is "admin" and show a button only admin can see
my jquery code is as below
$(document).ready(function () {
load_comment();
function load_comment() {// show comment function
$.ajax({
type: "POST",
url: "code.php",
data: {
't_id' : <?php echo $_GET['id']; ?>,
'comment_load_data': true
},
success: function (response) {
$('.comment-container').html("");
// console.log(response);
$.each(response, function (key, value) {
$('.comment-container').
append('<div class="reply_box border p-2 mb-2">\
<h6 class="border-bottom d-inline"> '+value.user['username'] +' | '+value.cmt['time'] +' </h6>\
<p class="para"> '+value.cmt['comment'] +' </p>\
<button value="'+value.cmt['ID'] +'" class="badge btn-warning reply_btn">Reply</button>\
<button id="view" value="'+value.cmt['ID'] +'" class="badge btn-danger view_reply_btn">View Replies</button>\
<?php $sql = "SELECT * FROM users WHERE role = 'admin';"; $result = $con->query($sql);if ($result->num_rows > 0) {while($row = $result->fetch_assoc()) {if ($row["role"] == "admin"){ echo "<button>Delete</button>";}}}?>\
<div class="ml-4 reply_section"></div>\
</div>\
');
});
}
});
}
The sql query is working fine in PHPMyAdmin but the button is not shown when I logged in as a admin This script is included in a PHP file
So you'll need to check if the current user is an admin. As M. Eriksson mentioned your current code will get all users with theadmin
role and print a delete button for each user found.
What you actually need to do is get the current users role and check if that it matches admin. Assuming the current user ID accessible somewhere in your code you could start by doing a query something like...
<?php
// CHANGE THIS TO YOUR CURRENT USER ID
$current_user_id = 1; // maybe get_current_user_id() ??
$statement = $con->prepare("SELECT * FROM users WHERE id = ?");
$statement->bind_param('i', $current_user_id);
$statement->execute();
// User specific variables
$user = $statement->fetch_assoc();
$is_admin = ($user['role'] ?? null) === 'admin';
?>
Once you have the above variables you can do something like...
// Create JS variables. (We print boolean values with `json_encode`).
const userIsAdmin = <?= json_encode($is_admin); ?>;
const postId = <?= (int)$_GET['id']; ?>;
(function($) {
const $container = $('.comment-container');
// Make ajax request.
$.ajax({
type: 'POST',
url: 'code.php',
data: {
t_id: postId,
comment_load_data: true
},
success: function (response) {
// Empty the container, for some reason it might have contents already ??\_(??�)_/??
$container.empty();
// Loop through each comment.
$.each(response, function (key, value) {
const comment = data?.cmt;
const author = data?.user;
// Create a comment box.
const $commentBox = $('<div>', {
class: 'reply_box border p-2 mb-2'
}).appendTo($container);
// Give the reply a title (and add it to the comment box).
$('<h6>', {
class: 'border-bottom d-inline',
text: `${user['username']} | ${comment['time']}`
}).appendTo($commentBox);
// Print the comment (and add it to the comment box).
$('<p>', {
class: 'para',
text: comment['comment']
}).appendTo($commentBox);
// Create a reply button (and add it to the comment box).
$('<button>', {
class: 'badge btn-warning reply_btn',
value: comment['ID'],
type: 'button',
text: 'Reply'
}).appendTo($commentBox);
// Create a view button (and add it to the comment box).
$('<button>', {
class: 'badge btn-danger view_reply_btn',
value: comment['ID'],
type: 'button',
text: 'View Replies'
}).appendTo($commentBox);
// Only create a delete button if the user is an admin.
if (userIsAdmin) {
$('<button>', {
class: 'badge btn-danger delete_btn',
value: comment['ID'],
type: 'button',
text: 'Delete'
}).appendTo($commentBox); // (and add it to the comment box)
}
// Now add this empty div for something?
$('<div>', {
class: 'ml-4 reply_section'
}).appendTo($commentBox);
});
}
});
// Reply button event.
$container.on('click', 'button.reply_btn', function(event) {
event.preventDefault();
const $button = $(this);
const commentId = $button.attr('value');
// Do something...
});
// View button event.
$container.on('click', 'button.view_reply_btn', function(event) {
event.preventDefault();
const $button = $(this);
const commentId = $button.attr('value');
// Do something...
});
// Delete button event.
$container.on('click', 'button.delete_btn', function(event) {
event.preventDefault();
const $button = $(this);
const commentId = $button.attr('value');
// Do something...
});
})(jQuery);
One thing to note that you will want to double check the user is an admin on the server side when processing the delete action. Simply hiding the button client side isn't good enough.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.