基于表单输入的Javascript重定向

时间:2013-10-12 03:01:01

标签: javascript php html forms

我需要使用表单值将一个页面重定向到另一个页面。

我有这个代码,我觉得第一页很好,我应该把它放在我要显示数据的另一个页面?

<meta http-equiv='refresh' content='0;url=http://site.com/page.php'>


<form action="http://site.com/page.php" method="post" name="myform">

<input type="hidden" name="url" value="<?php echo $url; ?>">

<script language="JavaScript">document.myform.submit();</script>
</form>

此致

3 个答案:

答案 0 :(得分:1)

您无法将元刷新重定向与表单提交本身混合使用。 而且,无论如何,meta-refreshes都很糟糕。由于您已经控制了接收页面,并且它使用PHP,因此使用它来完成重定向。试试这个:

<form action="http://site.com/page.php" method="post" name="myform">
<input type="submit" value="Go!" />
</form>

然后,在page.php中:

<?php
// Act on the input, store it in the database or whatever.  Then do the redirect using an HTTP 302.
header('Location: http://example.com');
?>

如果您需要表单将目标传递给page.php,您将需要对其进行清理以防止出现大量安全问题。这是一个粗略的轮廓。

<form action="http://site.com/page.php" method="post" name="myform">
<input type="hidden" name="destination" value="http://example.com" />
<input type="submit" value="Go!" />
</form>

然后,在page.php中(从答案https://stackoverflow.com/a/5085981/198299复制重新编码):

<?php
$destination = $_POST['destination'];
$url_parsed = parse_url($destination);
$qry_parsed = array();
parse_str($url_parsed['query'], $qry_parsed);
// Check that $destination isn't completely open - read https://www.owasp.org/index.php/Open_redirect
$query = parse_url($destination);
$destination = "{$url_parsed['scheme']}{$url_parsed['host']}{$url_parsed['path']}?" . http_build_query($query);
header('Location: ' . $destination);
?>

我还没有仔细检查过这段代码(只是在浏览器中写了这个代码),但它应该足够粗略。

答案 1 :(得分:0)

在site.com/page.php

<script>window.location.href = 'newPage.php';</script>

你必须在php标签之外写这个。

答案 2 :(得分:0)

要在PHP中重定向页面,请使用:

<?php
header('Location: url/file.php');
?>

刷新到HTML中的其他页面,请使用:

<meta http-equiv='refresh' content='0;url=http://url/file.php'>

在content属性中,0是等待的秒数。

刷新到JavaScript中的其他页面,请使用:

window.location.href = 'url/file.php';

如果这些都不起作用,请使用HTML跟随锚链接

<a href="url/file.php">Click here to go now!</a>


<小时/>

要回答您的问题,可以采取以下几种方式:

1)非常糟糕,需要两个文件,超级冗余
HTML文件:

<form action="http://site.com/page.php" method="post" name="myform">
    <input type="hidden" name="url" value="<?php=$url?>">
</form>
<script type="text/javascript">
    // Submit the form
    document.forms['myform'].submit();
</script>

page.php文件:

<?php
    // Catch url's value, and send a header to redirect
    header('Location: '.$_POST['url']);
?>

2)略好一点,仍然不推荐

<form action="http://site.com/page.php" method="post" name="myform">
    <input type="hidden" name="url" value="<?php=$url?>">
</form>
<script type="text/javascript">
    // Set form's action to that of the input's value
    document.forms['myform'].action = document.forms['myform'].elements['url'].value;
    // Submit the form
    document.forms['myform'].submit();
</script>

3)仍然非常多余,但我们正在变得更好

<form action="http://site.com/page.php" method="post" name="myform">
    <input type="hidden" name="url" value="<?php=$url?>">
</form>
<script type="text/javascript">
    // Simply refresh the page to that of input's value using JS
    window.location.href = document.forms['myform'].elements['url'].value;
</script>

4)好多了,省去了很多麻烦,只是首先使用JS

<?php
    // Start with a PHP refresh
    $url = 'url/file.php'; // Variable for our URL
    header('Location: '.$url); // Must be done before ANY echo or content output
?>
<!-- fallback to JS refresh -->
<script type="text/javascript">
    // Directly tell JS what url to refresh to, instead of going through the trouble to get it from an input
    window.location.href = "<?php=$url?>";
</script>
<!-- meta refresh fallback, incase of no JS -->
<meta http-equiv="refresh" content="0;url=<?php=$url?>">
<!-- fallback if both fail (very rare), just have the user click an anchor link -->
<div>You will be redirected in a moment, <a href="<?php=$url?>">or you may redirect right away</a>.</div>

使用.php扩展程序保存,您应该很高兴。

相关问题