代码之家  ›  专栏  ›  技术社区  ›  Samuel

不带表单发送POST数据

  •  29
  • Samuel  · 技术社区  · 14 年前

    例如,我是否可以将一个字符串或另一条信息发送到另一个.php文件,而不必使用表单(因此不是通过GET而是通过POST)公开它?

    5 回复  |  直到 14 年前
        1
  •  24
  •   Stephen Holiday    14 年前

    如果您不想让用户看到您的数据,请使用PHP会话。

    post请求中的数据仍然可以由用户访问(并可操作)。

    结帐 this tutorial 在PHP会话上。

        2
  •  17
  •   Darin Dimitrov    14 年前

    使用jquery $.post 方法很简单:

    $.post('/foo.php', { key1: 'value1', key2: 'value2' }, function(result) {
        alert('successfully posted key1=value1&key2=value2 to foo.php');
    });
    
        3
  •  10
  •   Gregor    13 年前

    使用会话发送数据,而不是post。

    session_start();
    $_SESSION['foo'] = "bar";
    

    $_POST['foo'] = $_SESSION['foo'];
    

    然后销毁会话(如果需要会话用于其他目的,则只需取消设置字段)。

    销毁会话或取消设置字段很重要,因为与POST不同,会话将保持有效,直到您明确销毁它或直到浏览器会话结束。如果你不这样做,你可以观察到一些奇怪的结果。例如:使用sesson过滤一些数据。用户打开过滤器并获取过滤后的数据。过了一段时间,他返回到页面并期望过滤器被重置,但事实并非如此:他仍然看到过滤后的数据。

        4
  •  7
  •   wpcoder    7 年前

    简单使用: file_get_contents()

    // building array of variables
    $content = http_build_query(array(
                'username' => 'value',
                'password' => 'value'
                ));
    // creating the context change POST to GET if that is relevant 
    $context = stream_context_create(array(
                'http' => array(
                    'method' => 'POST',
                    'content' => $content, )));
    
    $result = file_get_contents('http://www.example.com/page.php', null, $context);
    //dumping the reuslt
    var_dump($result);
    

    Reference

        5
  •  5
  •   Andreas Linden    14 年前

    看看php文档中的这些函数,您可以使用它们发送post-request。

    fsockopen()
    fputs()
    

    或者简单地使用 Zend\u Http\u客户端

    neat example 使用谷歌。。。

        6
  •  1
  •   user11713698    5 年前

    function redir(data) {
      document.getElementById('redirect').innerHTML = '<form style="display:none;" position="absolute" method="post" action="location.php"><input id="redirbtn" type="submit" name="value" value=' + data + '></form>';
      document.getElementById('redirbtn').click();
    }
    <button onclick="redir('dataToBeSent');">Next Page</button>
    <div id="redirect"></div>