使用onclick发布变量?

How can I $_POST information from one page to another by clicking a link? No forms or submit boxes, just if the end user clicks on a link, it will post pertinent information in the opened link.

Pseudocode, but:

//On first page
<a href="./page.php" onclick="post" value="var">Click me!</a>
...
//On the page the above links to
$variable = $_POST["var"];

Something that I considered is the following, which although less than pretty, works. I'd like it so it's a post and not a get.

//On first page
<a href="./page.php?var=<?php echo $variable; ?>">Click me!</a>
...
//On second page
$variable = $_GET["var"];

Try this :

<a href="link.php" class="post">submit content using link</a>

 <script type="text/javascript">
            jQuery(document).ready(function($){
                $(".post").on("click",function(){
                    $.ajax({
                        url: "http://www.yourwebsite.com/page.php",
                        type: "POST",
                        data: { name: "John", location: "Boston" },
                        success: function(response){
                              //do action  
                        },
                        error: function(){
                              // do action
                        }
                    });
                });
            });
        </script>

Reference Link

Not sure why you don't do this:

//On first page

<a href="./page.php?var=value">Click me!</a>

... //On the page the above links to

$variable = $_GET['var'];

UPDATE:

As per your above comment:

GET displays the information in the URL, whereas with POST it does not. I do not wish an end user to just edit the URL with whatever value they wish. @popnoodles, when the user would click the hyperlink, it would direct to a new page. – riista

You are trying to do this for security reasons, as such neither approach is a good way to do it using GET or AJAX is both unsafe as both can be tampered with. You need to re-think what you are trying to protect and how you can check if the data submitted is valid.

Add an click event listener and send the request to another page.

To choose between GET and POST is simple

  • POST should be used to modify state of the server.
  • all other cases goes for GET

So, you have to use GET in your case, not POST.
Security matters are irrelevant here.

you may also

$.get('./page.php?param=true&data=1', function(data){$('#content').append(data);});

You will need to wrap your "selections" in a form and use a button like so..

<style type="text/css">
/* just styling the overall menu */
#my-post-link ul{
  list-style-type:none;
  text-align:center;
  background-color:cadetblue;
  font-size:1.1em;
  font-weight:600;
  padding:0.45em;
  line-height: 1.75em;
}
/* following line would allow you to create a horizontal menu */
#my-post-link ul li{display:inline;white-space:nowrap;} 
#my-post-link button {
  background-color: transparent; /* <-- removes the gray button */
  border: none; /* <-- removes the border around the button */
  color: white;
  font-size: 1.15em;
  font-weight: 600;
}
#my-post-link button:hover {
  background-color: white;
  color: cadetblue;
  cursor: pointer; /* <-- makes the button text act like a link on hover */
}
</style>
<form id="myForm" name="myForm" method="post">
  <div id="my-post-link"><ul>
    <li><button name="select-me" type="submit" value="var" onclick=\"myForm.action='./page.php';return true;\">Click me!</li>
  </ul></div>
</form>

This will POST the value "var" to page.php with the key "select-me", so you would read that variable with...

$variable = $_POST["select-me"];

You could also further style the button(s) with pseudo classes to make them behave even more like a regular text link, but I think you get the idea.

If you want to see what variables are POSTing back to your page, you can drop this code in somewhere:

foreach ($_POST as $key => $value) {
  $i++;
  ${$key} = $value;
  echo "<!-- //[{$i}] {$key}:{$value} -->
";
}

You can also use var_dump($_POST) to view the POSTed data