How to pass POST parameters in a URL?

独自空忆成欢 提交于 2019-11-28 18:36:30
Petah

You could use a form styled as a link, no JavaScript required:

<form action="/do/stuff.php" method="post">
    <input type="hidden" name="user_id" value="123" />
    <button>Go to user 123</button>
</form>

CSS:

button {
    border: 0;
    padding: 0;
    display: inline;
    background: none;
    text-decoration: underline;
    color: blue;
}
button:hover {
    cursor: pointer;
}

See: http://jsfiddle.net/SkQRN/

Parameters in the URL are GET parameters, a request body, if present, is POST data. So your basic premise is by definition not achievable.

You should choose whether to use POST or GET based on the action. Any destructive action, i.e. something that permanently changes the state of the server (deleting, adding, editing) should always be invoked by POST requests. Any pure "information retrieval" should be accessible via an unchanging URL (i.e. GET requests).

To make a POST request, you need to create a <form>. You could use Javascript to create a POST request instead, but I wouldn't recommend using Javascript for something so basic. If you want your submit button to look like a link, I'd suggest you create a normal form with a normal submit button, then use CSS to restyle the button and/or use Javascript to replace the button with a link that submits the form using Javascript (depending on what reproduces the desired behavior better). That'd be a good example of progressive enhancement.

You can make an the link perform an ajax post request when it's clicked.

In jQuery:

$('a').click(function(e) {
   var $this = $(this);
   e.preventDefault();
   $.post('url', {'user': 'something', 'foo': 'bar'}, function() {
       window.location = $this.attr('href');
   });
});

You could also make the link submit a POST form with javascript

<form action="url" method="post">
   <input type="hidden" name="user" value="soemthing" />
   <a href="#">CLick</a>
</form>
<script>
$('a').click(function(e) {
   e.preventDefault();
   $(this).parents('form').submit();
});
</script>

I would like to share my implementation as well. It does require some javascript though.

<form action="./index.php" id="homePage" method="post" style="display: none;">
<input type="hidden" name="action" value="homePage" />
</form>
<a href="javascript:;" onclick="javascript:
document.getElementById('homePage').submit()">Home</a>

Nice thing about this, is that contrary to GET requests, it doesn't show the parameters on the URL, which is safer.

No you cannot do that. I invite you to read a POST definition: http://en.wikipedia.org/wiki/POST_%28HTTP%29

or this page: http://en.wikipedia.org/wiki/GET_%28HTTP%29#Request_methods

First off, a disclaimer: I don't think marrying POST with URL parameters is a brilliant idea. Like others suggested, you're better off using a hidden form for passing user information.

However, a question made me curious how PHP is handling such a case. It turned out that it's possible in theory. Here's a proof:

post_url_params.html

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <form method="post" action="post_url_params.php?key1=value1">
            <input type="hidden" name="key2" value="value2">
            <input type="hidden" name="key3" value="value3">
            <input type="submit" value="click me">
        </form>
    </body>
</html>

post_url_params.php

<?php
    print_r($_POST);
    print_r($_GET);
    echo $_SERVER['REQUEST_METHOD'];
?>

Output

Array ( [key2] => value2 [key3] => value3 ) 
Array ( [key1] => value1 ) 
POST

One can clearly see that PHP stores URL params in the $_GET variable and form data in the $_POST variable. I suspect it's very PHP- and server-specific, though, and is definitely not a thing to rely on.

This could work if the php script generates a form for each entry with hidden fields and the href uses javascript to post the form.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!