# URL redirect when submit button is clicked



## ebay258 (Dec 12, 2004)

Hello,
I'm trying to make the submit button on my email form redirect to a different URL when it's clicked and still submit the message. What's the code to do this and where do I put it? *THE FORM IS IN HTML*


----------



## Shadow2531 (Apr 30, 2001)

If you truly want to submit to one page and redirect to another, you are not going to like your options. The page you are submitting to should do the redirection.

However, you have 3 options if you only have html and javascript.

Submit to the one page in a new window and then change the location of the current window.

You do have to consider popup blockers, but if you are actually clicking to submit, the popup blocker should let it through unless the person has "block all popups" set for their browser.

The good thing about that way is the redirection doesn't interfere with the submission. You could autoclose the popup window after a certain time, but if it's closed too soon, it could interupt the tranfer.

Now the another way is to have an invisible iframe that you submit to. You use javascript for this to disable the normal form actions, submit to the iframe and then change the location after a certain amount of time.

That method will work, but the change of location can interupt the submission in the iframe.

The 3rd way and maybe the best way is to use the XML HTTP Request Object.

I've really never messed with it so you'll have to investigate that to see if it allows you do to what you want. It won't be supported in old browsers so if you have to support ancient browsers, this is not the way to go. The url I posted should definitely help. If you click the "loadcode button on that page, you'll set how it works and how you can use get or post to submit to the server.

Here's an example of the iframe way though that will give you some hints of how to control the forms. (tested only in opera). When you click submit, it will do a google search in the hidden iframe and then in 3 seconds, redirect you to www.hardocp.com.


```
<script type="text/javascript">
    function multi() {
        var obj = document.forms.searchform;
        var info = obj.action + "?" + obj.q.name + "=" + obj.q.value;
        frames['hide'].location = info;
        setTimeout("location = 'http://www.hardocp.com';", 3000);
    }
</script>
<form id="searchform" action="http://www.google.com/search" method="get" onsubmit="return false;">
    <div>
        <label for="search">Search Google</label>
        <input id="search" type="text" name="q" />
        <a href="javascript:multi()">Submit</a>
    </div>
</form>
<div style="visibility: hidden; width: 0px; height: 0px;">
    <iframe name="hide" src=""></iframe>
</div>
```
You'll have to change things to work for the pages you need to work with.

Edit:

I tried the request object method and it works, but in Opera at least, the get request uri must be on the same domain that the script is running. That means it will work, but not if the page you are submitting to is on a different domain.


----------

