Skip to content
Advertisement

How to get the value of while loop(PHP) using JavaScript?

I have this while loop in PHP

JavaScript

In this loop, when we click on the “delete” link, the value of variable “$post_id” changes. I want to get the value of “$post_id” when it changes. I tried with this JS code

JavaScript

But it only gives me the last value of “$post_id”. I want to get each value of “$post_id” when it changes and this value should be individual, not like this 23 46 545 545, etc. I don’t want to use any framework like jQuery.

Advertisement

Answer

  • An ID must be unique in a document. So stop using static IDs in a loop.
  • Links go somewhere. Don’t use a link if you aren’t navigating. Use a button if you want something to click on to trigger JS. Apply CSS if you don’t like the default appearance of a button.
  • A post ID is not a type of relationship. Don’t abuse rel attributes. (Appropriate use would be something like <a href="page2.html" rel="next">) Use a data-* attribute if you need to associate custom data with an element.
  • Intrinsic event attributes have a bunch of gotchas associated with them. Don’t use them. Use addEventListener and friends instead.

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement