1

I have a website with many posts displayed in the same page, where you can comment and react to every post. For storing the comment in database I need to know the post ID. To achieve this I set an attribute to submit button (post_id="123..") and access it with JS. Is there any way to know the post ID without storing it in an attribute? (the user can change the attribute with dev tool and mess with database record).

isherwood
  • 58,414
  • 16
  • 114
  • 157

1 Answers1

0

You can try like:

  1. You have array of all posts in js and you are able to access it based on index.
  2. Add same class name to your submit button and onClick of button write code to read index of clicked button based on class name you provided in HTML.
  3. Use this index to read any property from step1 array.

To read index of clicked element based class you can refer:

Get index of clicked element using pure javascript

Get index of clicked element in collection with jQuery

And other option you can look for is disabling right click and/or developer tool for browser.

Naresh J
  • 2,087
  • 5
  • 26
  • 39
  • i tried this way and i found out that all this system collapse if i simply delete a post or an element with dev tool. if i delete an element when i access it from its index the result i get is from the next or from the previus post. thanx for your answer! – Σπύρος Παναγιωτόπουλος Mar 11 '21 at 15:55