简体   繁体   中英

How to stop Redirecting a page when a form is submitted?

I'm using a form and when the form is submitted I used action attribute and called a server url where the form data gets saved

Here's how it looks :

<form name="example" action="webresources/data/update" method="post">
</form>

The above form when submitted updates the form data in to the server but at the same time it also takes me to the new page webresources/data/update which has nothing but server response

I don't want this response to be shown on the web page. The url should be called when the form is submitted and it should be on the same page without redirecting to a new page.

Is there any way to do this, I'm allowed to send the data only via form parameters.

Thank you in advance :)

Remove the action attribute in the form and put the button outside the form.

When onclick, make an ajax call.

<form>
stuff
</form>
<input type="button" onclick="ajax()">

<script>
function ajax(){}
</script>
<form name="example" action="webresources/data/update" method="post" onclick="return false">
</form>

Returning false will stop the page from reloading or refreshing.

I suggest that before you ask a question you do a quick google search because this was the first result that came up for me prevent form submit from redirecting/refreshing using javascript .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM