简体   繁体   中英

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. 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. 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. I would like to keep it an <a> tag because that is how the style is set.

Try: <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:

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

Maybe you can also rely on info from the http-referer headline.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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