/    Sign up×
Community /Pin to ProfileBookmark

Ajax call for list with buttons not working

I have a list in which at the end is a button called Approve.

My goal is when the button is pressed that a variable called reviewid is passed to the php script called updateapprovalDB.php.

The code is generally doing what I want, however all the php script is called multiple times once for each reviewid.

I only want it called for the reviewid for that button.

My code is shown below.

echo ‘<h1>Review Approvals</h1>’;
$con = mysqli_connect(‘www.myvirtualpersonaltrainer.com’, ‘fitnevq7_a8e’, ’09DCB8Fc4m6x1o2l3h7p5′, ‘fitnevq7_a8e’);
$sql = “SELECT ID,post_author,post_date,post_content,post_status FROM `a8e_posts` WHERE `post_type`=’review'”;
$result = $con->query($sql);
$count = 0;
while ($row = $result->fetch_assoc()) {
$postDate = ($row[‘post_date’]);
$postAuthor = ($row[‘post_author’]);
$UserLogin = GetUserLoginByID($postAuthor);
$postContent = ($row[‘post_content’]);
$postStatus = ($row[‘post_status’]);
$reviewID = ($row[‘ID’]);
if ($count == 0) {
echo “Post Date ” . “Author ” . “Content ” . “Status <br>”;
echo “========= ” . “====== ” . “======= ” . “====== <br>”;
}
echo ” $postDate $UserLogin $postContent $postStatus “;
if ($postStatus == “draft”) {
?>
<button>Approve</button>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js”></script>
<script>
$(document).ready(function(){
$(“button”).click(function(value,object){
var thisis = $(this);
var reviewid = $(this).attr(‘data-reviewid’);
$.ajax({
url: “updateapprovalDB.php”,
type: “POST”,
data: {‘action’:’ <?php echo $reviewID ?> ‘},
success: function(result){
//$(“#div1”).html(result);
$(“#”+reviewid).html(‘publish’);
console.log(‘true’);
thisis.remove();
thisis.hide();
console.log(‘fff’);
}
});
});
});
</script>
<?php
}
echo “<br>”;
}
}
?>
Below is my plugin page for approving reviews.

[upl-image-preview url=https://www.webdeveloper.com/forum/assets/files/2018-05-03/1525313237-691538-screenshot.png]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@gipperronauthorMay 07.2018 — @gipperron#1591750

I figure this out myself.

New code:

echo '<h1>Review Approvals</h1>';
$con = mysqli_connect('www.myvirtualpersonaltrainer.com', 'fitnevq7_a8e', '09DCB8Fc4m6x1o2l3h7p5', 'fitnevq7_a8e');
$sql = "SELECT ID,post_author,post_date,post_content,post_status FROM a8e_posts WHERE post_type='review'";
$result = $con->query($sql);
$count = 0;

while ($row = $result->fetch_assoc()) {

$postDate = ($row['post_date']);

$postAuthor = ($row['post_author']);

$UserLogin = GetUserLoginByID($postAuthor);

$postContent = ($row['post_content']);

$postStatus = ($row['post_status']);

$reviewID = ($row['ID']);


if ($count == 0) {
echo "Post Date " . "Author " . "Content " . "Status <br>";
echo "========= " . "====== " . "======= " . "====== <br>";

}
echo " $postDate $UserLogin $postContent "; echo '<strong id="'.$reviewID.'">- '.ucfirst($postStatus).'</strong>';
if ($postStatus == "draft") {
?>
<button id="approve_<?php echo $reviewID; ?>">Approve</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#approve_"+"<?php echo $reviewID; ?>").click(function(value,object){
var thisis = $(this);
$.ajax({
url: "updateapprovalDB.php",
type: "POST",
data: {'action':' <?php echo $reviewID ?> '},
success: function(result){
$("#"+"<?php echo $reviewID; ?>").html('- Publish');
thisis.remove();
thisis.hide();
}
});
});
});

</script>

<?php









}
echo "<br>";

}

}

?>
Copy linkTweet thisAlerts:
@rootMay 09.2018 — AJAX is Javascript, what you posted is not plain JavaScript but a Framework, it in itself my contain JavaScript but it is not JavaScript as a language...

Many people mistake these widgets, helper tools, frameworks and libraries as Javascript when they are not.

Please remember BBCode tags or use Markdown to separate your code from your question and it benefits you getting your answer quicker as people can read the code better.
×

Success!

Help @gipperron spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...