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:
<form id="add-to-cart" name="my-form" action="http://www.example.com" method="post">
to:
<form id="add-to-cart" name="my-form" action="http://www.page-2.com" method="post">
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")
<script type="text/javascript">
$(document).ready(function() {
// The link has class="linky-link" for easy selecting and href="http://www.page-2.com". I did not use an id because I had multiple nearly identical links on the same page.
$(".linky-link").on("click", function(e) {
e.preventDefault(); // Don't redirect yet!
var $form = $("#add-to-cart"); // $form instead of form because it's shorthand for a jQuery selector - not strictly necessary, but helpful
$form.append("<input type='hidden' name='link_redirect' value='" + this.href + "'>"); // This is added to the end of the form and triggers the redirect after the saving is done
$form.submit(); // Submitting the form saves the data, and then redirects
});
});
</script>
(Note: be sure this bit of JavaScript is located at the very bottom of the php file, once everything else is loaded)