1

I have a form that I would like to submit. Once done redirect the page to another. However currently it is not working for me.

<form name="myname" method="post" action="actionurl" onsubmit="gotonext()">

function gotonext(){
            var portalpath = window.location.pathname;                              
            var myredirect =  portalpath +"?uP_fname=msu/survey&amp;command=display&amp;sid=162";
            alert(myredirect);
            window.location.href = myredirect;
        } 

So the alert displays the correct url that I am seeking to go to. And the form submits the data to the correct actionurl. What is not working is the redirect.

Any ideas on where I might be going wrong?

user1689274
  • 373
  • 1
  • 4
  • 14

1 Answers1

2

Well, it's a bit more complicated. The problem is when the form is submitted, the client basically already starts processing a new request by URL specified in the form's action attribute.

One usual workaround is to use AJAX to submit the form, then trigger the redirect in that AJAX request's callback. That's easier, but has obvious limitations: AJAX requests won't cross domains.

Another approach (that might be useful in your case) is the following:

  • create a hidden iframe on the page with the form.
  • set the form's target attribute to that iframe's name
  • set onload event handler on this iframe, and trigger redirect in this handler.

Here's proof of concept.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • A Similar idea hit me last night, I will either do something like this, or add the link to the desired location as a hidden element on the page, then trigger a click with jquery, or use ajax...Thanks for the answer. and Thanks @RokoC.Buljan for the help as well – user1689274 Nov 01 '13 at 13:25