我在SO上看到了很多与此相关的其他问题,但没有一个对我有用。我正在尝试提交POST
表单,然后将用户重定向到另一个页面,但我不能让两者都发生。我可以获得重定向,也可以获得帖子,但不能同时获得。这就是我现在所拥有的:
<HTML>
<HEAD>
</HEAD>
<BODY>
<script type="text/javascript">
function redirect() {
window.location.href = "http://www.google.com";
}
</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>
我做错了什么?
答案 0 :(得分:0)
提交表单后,当前页面上的所有JS执行都会停止。您需要从服务器重定向,或让服务器吐出包含将执行重定向的JavaScript的页面。
答案 1 :(得分:0)
您可以使用 ajax 汇总表单,然后在收到服务器的响应后使用 javascript 重定向页面,或者如果您想要执行整页回发,那么您可以使用Location要使用 php 重定向的标头:
header('Location: http://somewebsite.com/somepage.php');
答案 2 :(得分:0)
考虑使用延迟覆盖来覆盖表单的默认提交行为。
$('#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.com");
});
});
答案 3 :(得分:0)
您可以通过Ajax请求发布表单,完成请求后将其重定向到新页面。
类似的东西:
<HTML>
<HEAD>
<script src="//ajax.googleapis.com/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.com";
});
});
});
</script>
</BODY>
</HTML>
答案 4 :(得分:0)
最好的方法是在提交表单后通过服务器端重定向。另一个是阻止默认表单提交,并使用ajax提交表单。这是示例代码:
<?php
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'production');
switch (ENVIRONMENT) {
case 'development':
error_reporting(-1);
ini_set('display_errors', 1);
break;
case 'testing':
case 'production':
ini_set('display_errors', 0);
if (version_compare(PHP_VERSION, '5.3', '>=')) {
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
} else {
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE);
}
break;
default:
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'The application environment is not set correctly.';
exit(1); // EXIT_ERROR
}
$system_path = 'system';
$application_folder = 'application';
$view_folder = '';
if (defined('STDIN')) {
chdir(dirname(__FILE__));
}
if (($_temp = realpath($system_path)) !== false) {
$system_path = $_temp.DIRECTORY_SEPARATOR;
} else {
$system_path = strtr(
rtrim($system_path, '/\\'),
'/\\',
DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
).DIRECTORY_SEPARATOR;
}
// Is the system path correct?
if (!is_dir($system_path)) {
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'Your system folder path does not appear to be set correctly. Please open the following file and correct this: '.pathinfo(__FILE__, PATHINFO_BASENAME);
exit(3); // EXIT_CONFIG
}
// The name of THIS file
define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));
// Path to the system directory
define('BASEPATH', $system_path);
// Path to the front controller (this file) directory
define('FCPATH', dirname(__FILE__).DIRECTORY_SEPARATOR);
// Name of the "system" directory
define('SYSDIR', basename(BASEPATH));
// The path to the "application" directory
if (is_dir($application_folder)) {
if (($_temp = realpath($application_folder)) !== false) {
$application_folder = $_temp;
} else {
$application_folder = strtr(
rtrim($application_folder, '/\\'),
'/\\',
DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
);
}
} elseif (is_dir(BASEPATH.$application_folder.DIRECTORY_SEPARATOR)) {
$application_folder = BASEPATH.strtr(
trim($application_folder, '/\\'),
'/\\',
DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
);
} else {
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'Your application folder path does not appear to be set correctly. Please open the following file and correct this: '.self;
exit(3); // EXIT_CONFIG
}
define('APPPATH', $application_folder.DIRECTORY_SEPARATOR);
// The path to the "views" directory
if (!isset($view_folder[0]) && is_dir(APPPATH.'views'.DIRECTORY_SEPARATOR)) {
$view_folder = APPPATH.'views';
} elseif (is_dir($view_folder)) {
if (($_temp = realpath($view_folder)) !== false) {
$view_folder = $_temp;
} else {
$view_folder = strtr(
rtrim($view_folder, '/\\'),
'/\\',
DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
);
}
} elseif (is_dir(APPPATH.$view_folder.DIRECTORY_SEPARATOR)) {
$view_folder = APPPATH.strtr(
trim($view_folder, '/\\'),
'/\\',
DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
);
} else {
header('HTTP/1.1 503 Service Unavailable.', true, 503);
echo 'Your view folder path does not appear to be set correctly. Please open the following file and correct this: '.self;
exit(3); // EXIT_CONFIG
}
define('VIEWPATH', $view_folder.DIRECTORY_SEPARATOR);
require_once BASEPATH.'core/CodeIgniter.php';