最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Send POST data along when link is clicked without using forms? - Stack Overflow

programmeradmin4浏览0评论

I have some regular anchor tag links on my page that open up a popup window which displays whatever GET data I pass through the url:

<a href="javascript:window.open('view.php?data=a%20bunch%20of%20data');">View</a>

Some of this data is really long, and my crappy web host has a very small limit on the size of the GET data that can be passed in a url. Is there a way to do this with POST data instead, without using html forms?

I have some regular anchor tag links on my page that open up a popup window which displays whatever GET data I pass through the url:

<a href="javascript:window.open('view.php?data=a%20bunch%20of%20data');">View</a>

Some of this data is really long, and my crappy web host has a very small limit on the size of the GET data that can be passed in a url. Is there a way to do this with POST data instead, without using html forms?

Share Improve this question asked Mar 10, 2012 at 3:58 JoeyJoey 11k16 gold badges42 silver badges56 bronze badges 4
  • I think it's time to find a new webhost. You pay for this too? – Blender Commented Mar 10, 2012 at 4:01
  • Agreed, they are terrible. Unfortunately it's too late to get my money back. But if you can avoid it, don't use inMotion. – Joey Commented Mar 10, 2012 at 4:04
  • That's quite ridiculous. Are you able to upload files properly using that webhost? – Blender Commented Mar 10, 2012 at 4:06
  • I haven't tried, I don't use any mutlipart/form-data forms... – Joey Commented Mar 10, 2012 at 4:07
Add a ment  | 

4 Answers 4

Reset to default 8

You would need to use forms. However, you could nest your hyperlink in a form:

<form method="post" action="somepage.php" id="form1">
<input type="hidden" name="field1" value="foo" />
<input type="hidden" name="field2" value="bar" />
<a href="somepage.php?field1=foo&field2=bar" onclick="document.getElementById('form1').submit(); return false;">Hyperlink</a>
</form>

I just ran into the same sort of dilemma and came up with this solution in jQuery:

<a href="#" id="post-link">Example</a>

<script type="text/javascript">
     $(document).ready(function () { 
          $('a#post-link').click(function() {
               $('body').append($('<form/>', {
                    id: 'form',
                    method: 'POST',
                    action: '#'
               }));

               $('#form').append($('<input/>', {
                    type: 'hidden',
                    name: 'field1',
                    value: 'foo'
               }));

               $('#form').append($('<input/>', {
                    type: 'hidden',
                    name: 'field2',
                    value: 'bar'
               }));

               $('#form').submit();

               return false;
          });
     } );
</script>

you could also use an XMLHttpRequest

http://www.w3schools./xml/xml_http.asp

One thing I learned during my long experience with Web development: if you run into limitation from your hosting, host your site somewhere else. Going around hosting limitation - whatever they are - wont't get you very far in the long run. Hosting is cheap, you can get a VPS with generous resources for very reasonable prices nowadays.

It's not worth it.

发布评论

评论列表(0)

  1. 暂无评论