【问题标题】:how to pass POST variable by links to own pages?如何通过链接传递 POST 变量到自己的页面?
【发布时间】:2011-10-16 18:08:36
【问题描述】:

嗨,我想通过链接到自我页面来获取变量 $_POST。示例:

<?PHP
$var = 'PIG';
echo "<a href='test.php?var=$var'>link</a>";

if (isset($_POST['var']))
{
echo $_POST['var']);
}


?>

它链接到自己的页面。 (test.php) 它不起作用,请谁可以帮助我。谢谢

【问题讨论】:

    标签: php post hyperlink


    【解决方案1】:

    链接不能POST数据,只能GET

    与只有 URL 和标头的 GET 请求方法不同 发送到服务器的 POST 请求还包括消息体。这 允许将任意长度的任何类型的数据发送到服务器。

    基本上,POST 需要两个请求,1) 服务器收到“正常”请求,带有一个额外的标头值,表明需要发送更多数据。此时,服务器发送一个确认,并且 2) 客户端发送 POST 正文。仅通过链接无法实现此行为。

    但是,有一些解决方案,我已经看到了一些技术,其中包括输出带有自动提交的form,类似于

    <form name="frm" method="post" action="http://your.domain.com/path/to/page.php?param1=1&param2=2">
    <input type="hidden" name="foo" value="bar" />
    </form>
    <script type="text/javascript">
        document.forms["frm"].submit();
    </script>
    

    这将导致使用这些参数调用page.php

    $_GET = array('param1' => '1', 'param2' => '2');
    $_POST = array('foo' => 'bar');
    

    请注意,这是一个简单的“重定向”方法,但您可以创建&lt;a&gt; 元素来实际触发一些隐藏的表单,而不是使用标准链接。 (未经测试的代码)

    <a href="http://your.domain.com/path/to/page.php?param1=1&param2=2" onclick="return dopost(this.href, {foo:'bar'});">A simple link</a>
    <script type="text/javascript">
       function dopost(url, params) {
           var pe = '';
           for (var param : params) {
               pe += '<input type="hidden" name="'+param+'" value="'+params[param]+'" />';
           }
           var frmName = "frm" + new Date().getTime();
           var form = '<form name="'+frmName+'" method="post" action="'+url'">'+pe+'</form>';
           var wrapper = document.createElement("div");
           wrapper.innerHTML = form;
           document.body.appendChild(wrapper);
           document.forms[frmName].submit();
       }
    </script>
    

    实际上,这可能是您所需要的。

    【讨论】:

      【解决方案2】:

      查询字符串中的项目可通过$_GETnot $_POST 获得,因为它们实际上并未发布。如果您想 POST,那么您必须使用带有 methodpost 的表单,或者您必须执行 XHR 作为 POST。

      【讨论】:

        【解决方案3】:

        不幸的是,你真的不能这样做。如果需要使用锚点提交值,则需要通过$_GET$_REQUEST 访问变量。

        如果它必须是 $_POST(如果您已确定该设计决策,因为 $_GET 实际上在那里更有意义),您可以使用表单和提交按钮的样式使其看起来非常像一个链接。将此代码放入文本编辑器并检查。

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
                              "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html>
            <head>    
                <style type="text/css">
                .button {border:none;background-color:#FFFFFF}
                .button:hover{ color:blue; }
                </style>
            </head>
            <body>
                <form action="test.php">
                    <input type="hidden" name="var" value="<?php echo $val; ?>" />
                    This kinda looks like a link: 
                       <input type="submit" value="link" class="button" />
                </form>
            </body>
        </html>
        

        【讨论】:

          【解决方案4】:

          如果您有多个链接并且不想全部重写,只需添加一个 fake 表单,如下所示:

          <form id="fakeForm" method="post">
              <input type="hidden" name="post_key" value="post_value" />
          </form>
          

          并设置正确的 jquery:

          $('a').click(function(event){
              event.preventDefault();
              $('#fakeForm').attr('action',$(this).attr('href')).submit();
          });
          

          在这种情况下,当您点击任何链接时,着陆页会收到post_value 变量。

          注意,如果不是左键点击链接(或者js被禁用),链接可以正常工作,但是值没有传递!

          【讨论】:

            【解决方案5】:

            下面这段代码演示了T30的想法。

            我通过 $_POST 传递的理由是防止某些变量在此处完成的 url 中暴露。但是,它们仍然会通过“查看源代码”暴露出来。

            <?php
            
            /*
            This demonstrates how to set $_POST from a link in .php without ajax based on the idea from http://stackoverflow.com/a/27621672/1827488. The rationale for doing so is to prevent certain variables ('userid') from being exposed in the url via $_GET. However, there does not seem to be a way to avoid those variables being exposed by 'view source'.
            */
            
            echo "<!DOCTYPE html><html lang='en'><head><title>Test Data Link</title></head><body>";
            
            // only one hidden form
            echo "<form class='hiddenForm' method='post'>
                    <input class='hiddenFormUserid' type='hidden' name='userid'/>
                  </form>";
            
            // as many links as you need
            echo "<p><a class='hiddenFormLink' href='?following=1' data-userid=101>Following</a> &bull; <a class='hiddenFormLink' href='?followers=1' data-userid=101>Followers</a></p>";
            echo "<p><a class='hiddenFormLink' href='?following=1' data-userid=102>Following</a> &bull; <a class='hiddenFormLink' href='?followers=1' data-userid=102>Followers</a></p>";
            echo "<p><a class='hiddenFormLink' href='?following=1' data-userid=103>Following</a> &bull; <a class='hiddenFormLink' href='?followers=1' data-userid=103>Followers</a></p>";
            
            echo "<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js'></script>";
            
            echo "<script type='text/javascript'>
                    console.log('jq');
                    $('.hiddenFormLink').click(function(e){
                      console.log('data-userid=' + $(this).attr('data-userid') + ', value=' + $('.hiddenFormUserid').val());
                      e.preventDefault();
                      $('.hiddenFormUserid')
                        .val($(this).attr('data-userid'));
                      $('.hiddenForm')
                        .attr('action',$(this).attr('href'))
                        .submit();
                    });
                  </script>";
            
            if (isset($_GET["following"]) || isset($_GET["followers"])) {
            
              if (isset($_GET["following"])) {
                echo "followed by ";
              } else {
                echo "followers of ";
              }
            
              if (isset($_POST["userid"])) {
                echo $_POST["userid"]."<br>";
              } else {
                echo "no post<br>";
              }
            
            } else {
              echo "no get<br>";
            }
            
            echo "</body></html>";
            
            $_POST["userid"] = "";
            
            ?>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-06-23
              • 1970-01-01
              • 2014-07-07
              • 1970-01-01
              • 1970-01-01
              • 2013-04-02
              相关资源
              最近更新 更多