简体   繁体   English

如何<a>使用表格</a>发送带有<a>标签的</a> POST数据

[英]How to send POST data with an <a> tag using a form

So I have something like this 所以我有这样的事情

<li><a href="new/page">Something</li>
<li><a href="#">Something else</li>

I want to be able to click one of the items, and send the current URL with so I can get it in the new page. 我希望能够单击其中一项,并使用发送当前URL,以便可以在新页面中获取它。 I cannot store the current URL in the session because I will be going to a different location that doesn't have the same session. 我无法将当前URL存储在会话中,因为我将转到另一个没有相同会话的位置。 This is why I think that I want to have some sort of POST data so I can just send the URL or location along with it. 这就是为什么我认为我想要某种POST数据,以便我可以随其发送URL或位置的原因。 I would like to keep it an <a> tag because that is how the style is set. 我想将其保留为<a>标记,因为这是设置样式的方式。

Try: <a href="new/page?variable=data"> . 试试: <a href="new/page?variable=data"> When you are ready to extract the variable on another page, you can use something like this: 当您准备将变量提取到另一页上时,可以使用如下代码:

var myData = getQueryVariable("variable");

if(myData){
  console.log(myData);//to verify it
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("?");

  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  console.log('Query Variable ' + variable + ' not found');
}

HTTP GET solution from PHP: 来自PHP的HTTP GET解决方案:

<li><a href="new/page?from=<?php echo $PHP_SELF?>" Something</li>

Maybe you can also rely on info from the http-referer headline. 也许您也可以依赖http-referer标题中的信息。

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

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