Skip to content
Advertisement

JavaScript function on tag is getting executed without calling and even I applied onClick event listener

I am trying to call a javaScript function on click tag. But the function is getting executed automatically when I didn’t click on the link and I have also applied the onClick event listener for the function call but still, it is getting executed

here is the tag

JavaScript

here is function

JavaScript

Advertisement

Answer

The reason why it runs without you executing or clicking it is because it is a php code. The php code is ran when the page is first loaded and the ‘onclick’ doesnt stop it. Javascript’s onclick event handles client side events, while PHP is server-sided.

In order to only run the php code when the link is clicked, you need to look into ajax calls or html forms, basically a way to communicate with your server

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