使用javascript使用HTML表单参数重定向URL

Redirect URL using HTML form parameters using javascript

本文关键字:使用 重定向 URL 参数 表单 javascript HTML      更新时间:2023-09-26

我有一个HTML页面,其中有隐藏的表单参数,我想重定向到另一个URL,只要我点击HTML页面传递相同的参数。我目前无法用id和密码重定向。

<html>
<body>
<script language="javascript">
function runReport()
{
document.mstr.submit();
}
</script>
<form name = "mstr" action = "www.xyz.com" method = "post">
<input type="hidden" name="uid" value="xyz" />
<input type="hidden" name="pwd" value="xyz" />
</form>
</body>
</html>

您可以用几种不同的方法重定向。一个是window.location:

window.location = 'newPage.php';

您可以将$_GET[]变量附加到url的末尾。


如果你想发布变量,你需要使用一个表单:

<form id="hiddenForm" action="newPage.php" method="post">
   <input type="hidden" name="uid" value="..."/>
   <input type="hidden" name="pwd" value="..."/>
</form>
Javascript:

document.getElementById("hiddenForm").submit();

然后在newPage.php中,你可以用$_POST[]:

访问你的变量
$uid = $_POST["uid"];
$pwd = $_POST["pwd"];

使用jquery的监听器双击(dblclick),你可以绑定到它,并在有人双击你的文档时提交你的from。这会将表单中的数据发送到表单的action url。

<!DOCTYPE html>
<html>
    <head>
        <title>XYZ</title>
        <script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
    </head>
    <body>
        <script>
            $(function() {
                $(document).on('dblclick', function() {
                    $('form[name="mstr"]').submit();
                });
            });
        </script>
        <form name="mstr" action="http://www.xyz.com" method="post">
            <input type="hidden" name="uid" value="xyz" />
            <input type="hidden" name="pwd" value="xyz" />
        </form>
    </body>
</html>

如果你的行动是在一个不同的url上,那么你现在所处的一个,你要么需要有http://https://,你也可以只使用//www.your-domain.com,这将模仿当前的网站。所以如果你现在在http,它会使用这个,https也一样