2013-07-26 39 views
16

後這是我的表單代碼:HTML表單重定向提交

<form enctype="multipart/form-data" name="upload-file" method="post" action="http://example.com/upload"> 
    <div class="formi"> 
     <input id="text-link-input" type="text" name="url" class="link_form" value="your link" onfocus="if(this.value==this.defaultValue) this.value='';" onblur="if(this.value=='') this.value=this.defaultValue;" /> 
     <input type="submit" value="OK" class="link_but" /> 
    </div> 
    <div class="upl" title="Upload"><img src="http://example.com/example.png" alt="" style="vertical-align:middle;"/>Upload 
     <input type="file" name="file" size="1" class="up" onchange = "document.getElementById('text-link-input').value = String(this.value).replace('C:\\fakepath\\','')"/> 
    </div> 
</form> 

現在,我想表單數據提交後,提交者重定向到我所選擇的任何頁面,但動作駐留在其他網站我無法編輯的地方。將數據提交給該網站後,是否可以將用戶重定向到任何頁面?

從一些谷歌搜索,這是我發現。將此添加到表單代碼中:

onSubmit=window.location='http://google.com' 

這沒有用。也許我沒有正確實施它?這是我做過什麼:

<form enctype="multipart/form-data" name="upload-file" method="post" onSubmit=window.location='http://google.com' action="http://example.com/upload"> 

另一個人說,加入一個隱藏字段應該工作:

<input type="hidden" name="redirect" value="http://your.host/to/file.html"> 

我應該如何落實,這是我的代碼?

建議和幫助等待...

+0

當你在表單控件將處理腳本中的隱藏字段會的工作,你的情況你就不用管了,這樣就不會工作 –

+0

形式第一次打擊將採取行動,並在行動中,你通過http://example.com/upload和形式willl去那是去這個URL –

+0

@Vickey是的,它去了example.com/upload,但我想它將數據提交給example.com/upload,然後轉到其他頁面。但是,我無法編輯example.com/upload來添加重定向。任何其他方式? – user2622661

回答

33

試試用這個Javascript(jquery)code。它對外部URL的ajax請求。使用回調函數來觸發任何代碼:

<script type="text/javascript"> 
$(function() { 
    $('form').submit(function(){ 
    $.post('http://example.com/upload', function() { 
     window.location = 'http://google.com'; 
    }); 
    return false; 
    }); 
}); 
</script> 
+2

想知道爲什麼這會得到downvoted?看起來像正確的方法給我(假設OP願意使用jQuery)可能需要一個'return:false;'在那個提交處理程序,雖然... –

+0

這是第一個解決方案+1 – Harshad

+1

如果你還想發送表單值,可以添加類似'$('#form_id')。serialize()'的東西作爲第二個參數發佈(在URL之後) – bsa