在没有表单的情况下将查询数据发布到 URL 的方法

Ways to post query data to URL without a form?

本文关键字:方法 URL 数据 表单 情况下 查询      更新时间:2023-09-26

我需要通过URL传递一些信息,只需单击链接,而不是使用带有表单和输入按钮的action="GET"。这可能吗?这只是客户端,没有服务器,因此在这种情况下,有关PHP等的建议将没有用。

在锚点中,更改 href 以在末尾包含查询字符串。

例如

 <a href="http://www.example.com/test.html?parameter=2">

假设您有权访问客户端上的变量,您可以执行以下操作:

<script type="text/javascript">
    navigateToPage = function(val){        
        window.location.href = "/somefolder/somefile.htm?id=" + val;
    }
</script>
<input type="button" value="Navigate" onclick="navigateToPage(5);" />

你需要使用 JavaScript 来获取所有值,然后将它们组合成一个 URL。

下面是一个示例(使用 jQuery 库):

<a href="http://example.com/test.html" id="paramLink">Click</a>
<script>
$(function(){
  // The data, from the page
  var id = 1, name = 'test';
  // Add event to link
  $('#paramLink').click(function(e){
    e.preventDefault(); // Stop the browser from following the link
    location.href = $(this).attr('href')+'?id='+id+'&name='+name; // Build the URL
  });
});
</script>

将您的信息放在锚点中并随之发布

<a href="yourpage.php?id=2&action=edit"> click! </a>

如何在重定向页面上获取这些值;如果您使用PHP。

$id=intval($_REQUEST['id']);
$action=$_REQUEST['action'];