admin管理员组文章数量:1357595
I've seen a lot of the other questions related to this on SO, but none have worked for me. I am trying to submit a POST
form and then redirect the user to another page, but I can't get both to happen. I can either get the redirect, or the post but not both. Here is what I have now:
<HTML>
<HEAD>
</HEAD>
<BODY>
<script type="text/javascript">
function redirect() {
window.location.href = "";
}
</script>
<form id="myForm" method=POST name=transferform
action="some_place.php" onsubmit="redirect();">
<input name=gold type=text value="1" size=5>
<input name=recipient type=text value="mypal" size=10>
<input id="myButton" type=submit name=submission value="Send">
</form>
<script type="text/javascript">
window.onload = function(){
document.getElementById('myButton').click();
};
</script>
</BODY>
</HTML>
What am I doing wrong?
I've seen a lot of the other questions related to this on SO, but none have worked for me. I am trying to submit a POST
form and then redirect the user to another page, but I can't get both to happen. I can either get the redirect, or the post but not both. Here is what I have now:
<HTML>
<HEAD>
</HEAD>
<BODY>
<script type="text/javascript">
function redirect() {
window.location.href = "http://www.google.";
}
</script>
<form id="myForm" method=POST name=transferform
action="some_place.php" onsubmit="redirect();">
<input name=gold type=text value="1" size=5>
<input name=recipient type=text value="mypal" size=10>
<input id="myButton" type=submit name=submission value="Send">
</form>
<script type="text/javascript">
window.onload = function(){
document.getElementById('myButton').click();
};
</script>
</BODY>
</HTML>
What am I doing wrong?
Share Improve this question edited Oct 7, 2013 at 19:56 fvrghl asked Oct 7, 2013 at 19:48 fvrghlfvrghl 3,7286 gold badges30 silver badges37 bronze badges5 Answers
Reset to default 1Once you submit a form all JS execution on the current page stops. You need to either redirect from the server, or have the server spit out a page containing JavaScript that will perform the redirect.
You can post the form via Ajax request and after plete the request redirect it on new page.
something like this:
<HTML>
<HEAD>
<script src="//ajax.googleapis./ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</HEAD>
<BODY>
<form id="myForm" method=POST name=transferform action="some_place.php" onsubmit="return false;">
<input name=gold type=text value="1" size=5>
<input name=recipient type=text value="mypal" size=10>
<input id="myButton" type=submit name=submission value="Send">
</form>
<script type="text/javascript">
$(function() {
$(document).on("submit", "#myForm", function(event){
var name = $('input[name="gold"]:first'),
recipient = $('input[name="recipient"]:first');
$.post( "some_place.php", { name: name, recipient: recipient })
.done(function( data ) {
window.location.href = "http://www.google.";
});
});
});
</script>
</BODY>
</HTML>
the best way to do this, is through server side redirection after the form submitted. Another is with preventing the default form submission and use ajax to submit the form. here is an example code:
$("#myform").submit(function(e) {
e.preventDefault();
var frm = $('#myform');
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function (data) {
console.log('data');
window.location.replace('your-url');
}
});
You can either sumbit the form using ajax then after receiving response from server redirect the page using javascript or if you want to do full page postback then you can use Location header to redirect using php:
header('Location: http://somewebsite./somepage.php');
Consider overriding the form's default submission behaviour using a Deferred override.
$('#myForm').submit(function (e) {
// prevent form submission
e.preventDefault();
var thisForm = $(e.currentTarget);
$.ajax({
// simulate form submission
type: thisForm.attr('method') || 'POST',
url: thisForm.attr('action') || window.location.href,
data: $.serialize(thisForm.data())
})
.always(function () {
// when it is done submitting data to the server, redirect
window.location.replace("http://www.google.");
});
});
本文标签: javascriptSubmit a form and Redirect the pageStack Overflow
版权声明:本文标题:javascript - Submit a form and Redirect the page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744040766a2580618.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论