我有 2 个 php 文件。其中一个具有变量值,另一个使用这些值。我正在使用 post 将值从一个文件传递到另一个文件。
<html><body>
<form method="POST">
<input type="hidden" name="firstname" value="Saurabh">
<input type="hidden" name="age" value="22">
<input type="submit" onclick="javascript:completeAndRedirect();">
</form>
<script>
function completeAndRedirect(){
var r = confirm("Press a button!");
if (r == true) {
location.href='page2.php';
}
}
</script>
</body></html>
<?php
//Using POST
$firstname = $_POST['firstname'];
$age = $_POST['age'];
?>
<html>
<head>
</head>
<body>
<p>
first name : <?php echo "$firstname"; ?> <br>
age : <?php echo "$age"; ?>
</p>
</body>
</html>
但它没有按照我想要的方式工作。
仅当单击“确定”时才应重定向
它不会在任何地方重定向。
最佳答案
我收到了您的问题,请设置表单操作并将位置放入 window.href。
请替换以下代码。
<html><body>
<form method="POST" action="javascript:void(0);" name="myForm" id="myForm">
<input type="hidden" name="firstname" value="Saurabh">
<input type="hidden" name="age" value="22">
<input type="submit" onclick="javascript:completeAndRedirect();">
</form>
<script>
function completeAndRedirect(){
var r = confirm("Press a button!");
if (r == true) {
document.getElementById("myForm").action = "page2.php";
document.getElementById("myForm").submit();
}
}
</script>
</body></html>
关于javascript - 确认并发布值后重定向到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35083626/