简体   繁体   English

将数据传递到另一个页面并在该页面上提交表单

[英]Pass data to another page and submit form on that page

I'd like a button on page1.html.我想要 page1.html 上的按钮。 This button will pass data to page2.html which has a form on it, load that data into the form, and submit the form on page2.html.该按钮会将数据传递给 page2.html,它上面有一个表单,将数据加载到表单中,然后在 page2.html 上提交表单。 Is this possible?这可能吗? How would it be done?怎么做? I can't seem to find any examples of this.我似乎找不到任何这方面的例子。

Thanks!谢谢!

Of course the best thing to do is to use a backend... but I guess you wouldnt be asking if you had that option. 当然,最好的办法是使用后端...但是我想您不会问您是否有这种选择。

one thing you can do is use the anchor in the URI, like: /page2#?field=value&fld=val 您可以做的一件事是在URI中使用锚点,例如: /page2#?field=value&fld=val

Then on page load, check for an anchor tag and process it: 然后在页面加载时,检查定位标记并对其进行处理:

$(function(){
  let hash = window.location.hash.substr(1);
})

Here's a plunkr to get you off the ground: https://plnkr.co/edit/u9fPGFZedXGmKbbdXVvy?p=preview 这是让您动手的笨拙的人: https ://plnkr.co/edit/u9fPGFZedXGmKbbdXVvy?p=preview

So once you have the hash you would parse it to JSON and configure your form with it. 因此,一旦有了hash ,就可以将其解析为JSON并使用它配置表单。

It isn't obvious that it works because of how these fiddle-like sites operate. 由于这些类似于小提琴的网站是如何运作的,因此它尚不明显。 But it does work! 但这确实有效! To check it out, open the preview in separate window mode, and copy the url, open that url in a new tab, and add a sample hash on it, and press enter so it takes. 要检出它,请在单独的窗口模式下打开预览,然后复制URL,在新选项卡中打开该URL,并在其上添加示例哈希,然后按Enter键。 then reload the page. 然后重新加载页面。

I ended up using PHP's Post method: 我最终使用了PHP的Post方法:

From originating page1.php page: 从原始page1.php页面:

<form action="page2.php" method="post">
     <input type="hidden" name="MyName" id="MyId" value="MyVal">
     <input type="submit" value="Post to page2">
</form>

In receiving page2.php page: 在接收page2.php页面时:

<?php if (!empty($_POST)) : ?> // Check for Post data
    <script>
       document.getElementById("MyId").value = '<?php echo $_POST["MyName"]; ?>'; //update form on page2.php by field ID
       document.getElementById('FormToSubmit').click();
    </script>
<?php endif;?>

Works perfectly 完美运作

 You can simply add (target="_blank") to form tag and you can get data in specific action page
 <form action="test.php" method="post" target="_blank">
    <input type="text" name="test" />
    <button type="submit" name="submit">Submit</button>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM