Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to submit an HTML form without redirection

If I have a form like this,

<form action="/Car/Edit/17" id="myForm" method="post" name="myForm"> ... </form> 

how can I submit it without redirecting to another view by JavaScript/jQuery?

I read plenty of answers from Stack Overflow, but all of them redirect me to the view returned by the POST function.

like image 804
Duke Nuke Avatar asked Sep 22 '14 21:09

Duke Nuke


People also ask

Can we submit form without button?

The form can be submitted without using submit button by implementing a specific event attribute or by clicking the link. This task can be done by using the OnClick event attribute or by using the form. submit() method in Javascript.

How do you redirect to another page after form submit HTML?

If you want to redirect to another page after form submit html, Then you have to provide/Sign the Other pages path inside HTML Form tag's ACTION Attribute. Which will POST/Send your Form data to that Location and Open/Redirect your Users to That Given Web Page.

Can I use javascript to submit a form?

The method form. submit() is used for dynamic creation and sending the details to the server. The JavaScript form submission can be used for object creation and various attributes can also be used. The attributes can be class, id, tag, etc.

What triggers form submit?

Hitting enter on text fields can sometimes trigger a form submit. See here. Especially if that is the only element in the form. One way to control the post back is to set the action to empty and fire off the event yourself with Javascript.


2 Answers

You can achieve that by redirecting the form's action to an invisible <iframe>. It doesn't require any JavaScript or any other type of scripts.

<iframe name="dummyframe" id="dummyframe" style="display: none;"></iframe>  <form action="submitscript.php" target="dummyframe">     <!-- Form body here --> </form> 
like image 187
Issa Chanzi Avatar answered Sep 19 '22 08:09

Issa Chanzi


In order to achieve what you want, you need to use jQuery Ajax as below:

$('#myForm').submit(function(e){     e.preventDefault();     $.ajax({         url: '/Car/Edit/17/',         type: 'post',         data:$('#myForm').serialize(),         success:function(){             // Whatever you want to do after the form is successfully submitted         }     }); }); 

Also try this one:

function SubForm(e){     e.preventDefault();     var url = $(this).closest('form').attr('action'),     data = $(this).closest('form').serialize();     $.ajax({         url: url,         type: 'post',         data: data,         success: function(){            // Whatever you want to do after the form is successfully submitted        }    }); } 

Final solution

This worked flawlessly. I call this function from Html.ActionLink(...)

function SubForm (){     $.ajax({         url: '/Person/Edit/@Model.Id/',         type: 'post',         data: $('#myForm').serialize(),         success: function(){             alert("worked");         }     }); } 
like image 24
Amin Jafari Avatar answered Sep 22 '22 08:09

Amin Jafari