0

The site has to send POST request "amount=1" by klicking any Link on this site.

With GET this works:

window.onload = function () { document.body.onclick = function (e) { e = e||event; var was = (e.target||e.srcElement);
if ( was.href ) { was.href = was.href + '?edit=yes'; } }; };

but i dont want to have the parameter in the url.

My idea was this:

<form action="" id="amount" method="POST">
<input name="amount" type="hidden" value="1"/>
<input type="submit" />
</form>


<script>
window.onload = function () { document.body.onclick = function (e) { e = e||event; var was = (e.target||e.srcElement);
if ( was.href ) {          document.amount.submit();        } }; };

But this is not working.

kolja
  • 505
  • 7
  • 24

1 Answers1

0

Alright, onclick won't work always work see mouse events

Thus, the following will work for you.

window.onload = function() {
  document.body.onmouseup = function(e) {
      document.getElementById("amount").submit();
    }
};
<body>
  <form action="" id="amount" method="POST">
    <input name="amount" type="hidden" value="1" />
    <input type="submit" />
  </form>


  <span>this is in the body</span>

</body>
Chiel
  • 1,324
  • 1
  • 11
  • 30
  • Thank you, but your code won´t work always too. Often just one time after page refresh. – kolja Apr 12 '20 at 17:01
  • @kolja I can only see that problem if I run that fiddle. See this [working example](https://www.chielchiel.nl/test/) with the exact same code as above – Chiel Apr 12 '20 at 18:24