send $_POST data via anchor tag

冷暖自知 提交于 2019-12-30 07:50:49

问题


is it possible to somehow send $_POST[] data via a <a> tag? because it seems to automaticly execute the $_POST[] once the page loads rather than when I press the Anchor tag.

edit: so what I'm trying to achieve is something like this:

I want an anchor tag which would normaly go to somepage.php?variable=something but instead of using $_GET[] I want to send the variable via $_POST


回答1:


You can achieve this using jQuery and a HTML form

HTML:

<form method="post" name="redirect" class="redirect">
<input type="hidden" class="post" name="post" value="">
<input type="submit" style="display: none;">
</form>

Button: (html)

<a href='javascript:void(0)' class='button' var='DATAHERE'>sometexthere</a>

Javascript, or rather said jQuery:

$(".button").click(function() {
    var link = $(this).attr('var');
    $('.post').attr("value",link);
    $('.redirect').submit();
});

this jQuery code listen's to any clicks on the items with the class button attached to them, and reads out their "var" value, basicly you could use any kind of HTML element using this method as long as they have the button class attached to it.




回答2:


Nothing in HTML will cause a link to trigger a POST request or encode data in the request body.

You can bind a JavaScript event handler to a link, cancel the default behaviour and then send a POST request by programmatically submitting a form or using XMLHttpRequest. This generally isn't a good idea and you should use a submit button instead (which you can style to look like a link if you really, really want to).




回答3:


Answer is no. What you can do is set the value of an input type when the <a> tag gets clicked. Using Javascript.




回答4:


You could get hold of the query string from the href attribute of the anchor tag (you would need to parse the href and get hold of the string on the right hand side of the & symbol) and then post it using javascript or jquery (easier to use jquery).

http://api.jquery.com/jquery.post/

Would have to ask why you would want/need to do this?



来源:https://stackoverflow.com/questions/26629658/send-post-data-via-anchor-tag

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