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.
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.
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.
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.
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.
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>
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 } }); }
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"); } }); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With