Make a link use POST instead of GET
You create a form with hidden inputs that hold the values to be posted, set the action of the form to the destination url, and the form method to post. Then, when your link is clicked, trigger a JS function that submits the form.
See here, for an example. This example uses pure JavaScript, with no jQuery — you could choose this if you don't want to install anything more than you already have.
<form name="myform" action="handle-data.php" method="post">
<label for="query">Search:</label>
<input type="text" name="query" id="query"/>
<button>Search</button>
</form>
<script>
var button = document.querySelector('form[name="myform"] > button');
button.addEventListener(function() {
document.querySelector("form[name="myform"]").submit();
});
</script>
hyperlink a href using POST Method instead of GET method
The post method can only be used by form data if called by html.
So the only solution would be to make a form and hide the inputs which you want to post, and submit the post to the url you want to.
Making href (anchor tag) request POST instead of GET?
Using jQuery it is very simple, assuming the URL you wish to post to is on the same server or has implemented CORS
$(function() {
$("#employeeLink").on("click",function(e) {
e.preventDefault(); // cancel the link itself
$.post(this.href,function(data) {
$("#someContainer").html(data);
});
});
});
If you insist on using frames which I strongly discourage, have a form and submit it with the link
<form action="employee.action" method="post" target="myFrame" id="myForm"></form>
and use (in plain JS)
window.addEventListener("load",function() {
document.getElementById("employeeLink").addEventListener("click",function(e) {
e.preventDefault(); // cancel the link
document.getElementById("myForm").submit(); // but make sure nothing has name or ID="submit"
});
});
Without a form we need to make one
window.addEventListener("load",function() {
document.getElementById("employeeLink").addEventListener("click",function(e) {
e.preventDefault(); // cancel the actual link
var myForm = document.createElement("form");
myForm.action=this.href;// the href of the link
myForm.target="myFrame";
myForm.method="POST";
myForm.submit();
});
});
PHP link with post instead of get
Most email clients do not support form submissions or any form of Javascript. And you should always assume that people block scripts/images from emails by default. Clicking a link in an email will usually open the user's web browser and redirect them to the page - this way you can attach a query string with ?foo=bar
and access these parameters via $_GET
.
If you absolutely must retrieve these parameters in $_POST
, for whatever reason, you could redirect the user to a form that auto-submits as POST via Javascript (and pre-fill some hidden inputs to have your parameters passed through).
REST API using POST instead of GET
You can't use the API
using POST
or GET
if they are not build to call using these methods separetly. Like if your API say
/service/function?param1=value1¶m2=value2
is accessed by using GET
method. Then you can not call it using POST
method if it is not specified as POST
method by its creator. If you do that you may got 405 Method not allowed
status.
Generally in POST
method you need to send the content in body with specified format which is described in content-type
header for ex. application/json
for json data.
And after that the request body gets deserialized at server end. So you need to pass the serialized data from the client and it is decided by the service developer.
But in general terms GET
is used when server returns some data to the client and have not any impact on server whereas POST
is used to create some resource on server. So generally it should not be same.
How do I make a link use POST method in Yii?
Try this:
<?= Html::a('submit', Url::to(['site/index']), ['data-method' => 'POST']) ?>
Is there a way to make g:link calls POST instead of GET?
1.My suggestion is to use Ajax before the g:link it self handels the call to the action and controller
like:
<g:link class="btn btn-success" id="class.id" onclick='UsingPost();'>SOME LABLE HERE </g:link>
<script type='text/javascript'>
function UsingPost(){
jQuery.ajax({
type:'POST',
data:{"model":${pleaseUseTheModelyouHaveLoaded}"},
url:'${createLink(action: 'save')}',
success:function(data,textStatus){
jQuery('#success').html(data);},
error:function(XMLHttpRequest,textStatus,errorThrown){}
});
}
</script>
or refer to this post
https://stackoverflow.com/questions/17360606/grails-spring-security-core-ajax-authentication
When do you use POST and when do you use GET?
Use POST
for destructive actions such as creation (I'm aware of the irony), editing, and deletion, because you can't hit a POST
action in the address bar of your browser. Use GET
when it's safe to allow a person to call an action. So a URL like:
http://myblog.org/admin/posts/delete/357
Should bring you to a confirmation page, rather than simply deleting the item. It's far easier to avoid accidents this way.
POST
is also more secure than GET
, because you aren't sticking information into a URL. And so using GET
as the method
for an HTML form that collects a password or other sensitive information is not the best idea.
One final note: POST
can transmit a larger amount of information than GET
. 'POST' has no size restrictions for transmitted data, whilst 'GET' is limited to 2048 characters.
POST from an A tag
There is no way to POST
an a
element using only HTML.
As can be seen from this DTD fragment (HTML 4.01 spec):
<!ELEMENT A - - (%inline;)* -(A) -- anchor -->
<!ATTLIST A
%attrs; -- %coreattrs, %i18n, %events --
charset %Charset; #IMPLIED -- char encoding of linked resource --
type %ContentType; #IMPLIED -- advisory content type --
name CDATA #IMPLIED -- named link end --
href %URI; #IMPLIED -- URI for linked resource --
hreflang %LanguageCode; #IMPLIED -- language code --
rel %LinkTypes; #IMPLIED -- forward link types --
rev %LinkTypes; #IMPLIED -- reverse link types --
accesskey %Character; #IMPLIED -- accessibility key character --
shape %Shape; rect -- for use with client-side image maps --
coords %Coords; #IMPLIED -- for use with client-side image maps --
tabindex NUMBER #IMPLIED -- position in tabbing order --
onfocus %Script; #IMPLIED -- the element got the focus --
onblur %Script; #IMPLIED -- the element lost the focus --
>
There is no attribute that controls whether to use POST
or GET
with an a
element.
You have to script it, if you want to abuse the semantics.
Related Topics
Reading File Contents on the Client-Side in JavaScript in Various Browsers
Make Iframe Automatically Adjust Height According to the Contents Without Using Scrollbar
How to Programmatically Tell an HTML Select to Drop Down (For Example, Due to Mouseover)
Get Class List For Element With Jquery
Jquery Id Selector Works Only For the First Element
How to Change an Element'S Text Without Changing Its Child Elements
Clearing ≪Input Type='File' /≫ Using Jquery
Onclick to Get the Id of the Clicked Button
How to Stop Event Propagation With Inline Onclick Attribute
Using an HTML Button to Call a JavaScript Function
"Submit Is Not a Function" Error in JavaScript
Full-Screen Iframe With a Height of 100%
Rendering HTML Inside Textarea
Converting HTML String into Dom Elements
How to Get Value of Selected Radio Button
How to Get the Dom Element Which Contains the Current Selection