Click here to Skip to main content
14,303,166 members
Rate this:
Please Sign up or sign in to vote.
See more:
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:

    var plus = $(this).val();
          url: "../inc/handlers/up-vote.php",
          data: $(".upVote").serialize(),
          type: "POST",
          dataType: 'json',
          success: function (e) {
      return false;

The other page:
Updated 22-May-19 1:28am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You need to serialize the specific form that was submitted, not every form with the upVote class.
data: $(this).serialize(),
Galarist_00 22-May-19 8:08am
Thank you so much!!!! This really helped me out :))
0x01AA 24-May-19 13:38pm
Works for me
Richard Deeming 24-May-19 13:39pm
This solution, or posting comments[^]?

EDIT: Of course, immediately after reporting a problem with posting comments, posting comments starts working again. 🤦‍♂️
Galarist_00 24-May-19 13:43pm
What happened??? I am confused here lol...
Richard Deeming 24-May-19 13:46pm
Don't worry - there was a glitch in the matrix, which resolved itself almost as soon as I reported it. 0x01AA replied to my solution as a test of the glitch. :)
Galarist_00 24-May-19 13:57pm
oh :o what was the glitch?
Richard Deeming 24-May-19 13:59pm
I couldn't post solutions or comments. It seemed to be going on for a while. And then as soon as I reported it, it resolved itself.
Galarist_00 24-May-19 15:13pm
I see. Can I ask one more little help about this question? please. :)
0x01AA 24-May-19 14:07pm
Very sorry for the confusion. Simply ignore my post above here. It was only to prove whether Q/A has a problem or not. My post above has no relation to youe question.
Sorry again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100