I have a post system that displaying with a
foreach loop
. I try to implement
AJAX
for a button to avoid page refresh.
My real problem is when I do AJAX POST to pass a value from the PHP
foreach loop
then it will only pass one value from the PHP
foreach loop
(the last value).
I want to pass a value from a foreach loop to the page 2.
What I have tried:
The
form
from the
foreach
loop:
<form class="upVote" action="../inc/handlers/up-vote.php" method="post">
<input type="hidden" name="plus" value="'.$postIdentifier[$commentID].'" />
<button id="plus" type="submit" value="'.$postIdentifier[$commentID].'" ></button></form>
The
AJAX
code:
$(document).ready(function(){
$(".upVote").submit(function(){
var plus = $(this).val();
$.ajax({
url: "../inc/handlers/up-vote.php",
data: $(".upVote").serialize(),
type: "POST",
dataType: 'json',
success: function (e) {
console.log(JSON.stringify(e));
},
error:function(e){
console.log(JSON.stringify(e));
console.log('error');
}
});
return false;
});
});
The other page:
$_POST['plus'];