![](/img/trans.png)
[英]How can I pass values from one html page to another html page using javascript
[英]How can I display something from another page by using HTML/Javascript?
我四处寻找这个问题的答案,但要么不理解另一个答案的逻辑,要么在尝试合并这些答案时做错了什么。
我有2页HTML,第一页有这个表格
<form class='registerbutton' action='registration.php' method = "POST">
<input type='email' name='email' placeholder='Email'>
<input type='submit' value='Register'>
<script> localStorage.setItem("useremail",email);</script>
</form>
当我不使用 method="POST" 时,我可以在 URL 中看到 email 值被添加到 URL 中,因此这种形式有效。 但是,在我的下一页中,我有这个:
<body>
<script>
var test = LocalStorage.getItem("useremail");
document.write(test);
</script>
</body>
但是,这不起作用。 我也试过<?php echo email;?>
但这也行不通。 如果这是一个微不足道的问题,我很抱歉。 我更习惯于其他编程语言,并且对 web 开发不熟悉。
由于您的方法是 POST,因此您可以通过以下方式使用 PHP 访问您的 email:
<?php echo $_POST['email']; ?>
此解决方案仅适用于客户端。 试试它应该可以正常工作,它应该可以正常工作。 如果还有其他事情,请告诉我。
第 1 页
<form class='registerbutton' action='registration.php' method="POST">
<input type='email' name='email' placeholder='Email' id="emailId">
<input type='submit' value='Register'>
</form>
<script type="text/javascript">
var email = document.getElementById('emailId');
email.addEventListener("keyup", function(event) {
localStorage.setItem('useremail', event.target.value);
});
</script>
第2页
<body>
<script type="text/javascript">
var test = localStorage.getItem("useremail");
document.write(test);
</script>
</body>
服务器端
<?php
echo $_POST['email']; // In case of post and $_REQUEST['email'] will work for both.
?>
<script>
var test = localStorage.getItem("useremail");
document.write(test);
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.