I have a form like this
<form id="add-to-cart" name="my-form" action="http://www.example.com" method="post">
<div class="row flat">
<input type="hidden" name="cartkey" value="">
<input type="hidden" name="id" value="2">
<button type="submit" value="submit" data-value="2" data-name="id">Buy Now</button>
</div>
</form>
To Save the form I have used jQuery. So for now my code looks like this
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('button').click(function(){
var Value = jQuery(this).attr('data-value');
jQuery('[name="id"]').val(Value);
jQuery('#add-to-cart').submit();
window.location = "http://www.page-2.com";
});
});
</script>
But here after submit the form is not redirecting to another page(http://www.page-2.com). So can someone kindly tell me how to do redirect the form after submit the form?
Update Sorry here I can't use ajax. I want to submit the form in simple.
You can handle this in your action, you calling
onSubmit()
from server code redirect it to the page you want.Or you need to use ajax post() calls which give you onSuccess and onFailure events on call complete. Something like this:
I did it this way:
html:
Javascript:
I wrote above code for getting values from server, but you can change jQuery "Type" parameter to "POST" and add "data" parameter with its values you want to be sent to server.
Hope it helps you...
If you don't use ajax, the form will be submitted, and the browser redirected to the action url of the form. The only way to do a redirection after the submit, is to do it in the handler of the action form. You could however add a parameter with the URL you want to redirect to.
The only advantage of that solution over a solution involving ajax is that no javascript is required. It would work whether javascrit is enabled or not.
Disable the form submission using onsubmit="return false;"
html
js
keep the action attribute value as a fallback, so you'll be sure that your form will work correctly even if javascript is enabled or not on the client side.
I know that this is an old question, but I found a different way to redirect after submitting a form.
First of all, to reiterate the basics of the
<form>
tag since the OP was literally a NewUser, submitting a form has redirecting built into it. If you change your original html from:to:
then clicking on your submit
<button>
will automatically redirect you.However, maybe NewUser wants submitting the form to always stay on www.example.com and only sometimes redirect to www.page-2.com, once the submission is complete? I ran into this exact scenario at work today.
In my case, we had a form that consisted of data in a table. Users could make changes to the data and then click the submit button (called "Save") to save the changes in the database. The action of
<form>
just refreshed the form/table page because we wanted users to stay on that page.We also had a link on that page that would take you to another page, but I had to make sure that it autosaved all the data before redirecting. So I made the link submit the form, and then redirect. This was accomplished with jQuery, and without Ajax. Here it is, adapted to NewUser's code:
(
<form>
is unchanged, action="http://www.example.com")(Note: be sure this bit of JavaScript is located at the very bottom of the php file, once everything else is loaded)