Prevent Refresh of Page (Button Click)

Prevent refresh of page when button inside form clicked:

Actual Code:

<form method="POST">
    <button name="data" onclick="getData()">Click</button>
</form>

SOL1:

Add type="button" to the button. Like this,

<button name="data" type="button" onclick="getData()">Click</button>


SOL2:

Instead of using button tag, use input tag. Like this,

<input type = "button" name="data" onclick="getData()" value="Click">


Note: The default value of "type" for a button is "submit", which self posts the form in this and makes it look like a refresh.

Comments