duanjiani6826 2014-02-15 02:22
浏览 37
已采纳

了解如何在PHP中处理POST表单

This is more of an "understanding" question that a specific example. I'm coming from a Python/R/Scientific Computing background where I usually compile things or communicate through an interpreter. But everything is typically on the machine so there's no "communication" between server/client.

I'm now trying to learn PHP (in the hopes of letting users run my scripts from a web interface) and am curious what happens during a POST.

Consider the following script:

<!DOCTYPE html>
<html>
<head>
<title>Form Testing</title>
<meta charset="utf-8"/>
</head>
<body>

<?php
  if($_POST['formSubmit'] == "Submit")  {
    echo "Post Status is: ".$_POST['formSubmit']."
";
    $varMovie = $_GET['formMovie'];
     echo "Your Favorite Movie Was: ".$varMovie;
  }else{
  echo "Post Status is: ".$_GET['formSubmit'] ."
";

  }
?>

<form action="index.php" method="post">
    Which is your favorite movie?   
    <input type="text" name="formMovie" maxlength="50">
    <input type="submit" name="formSubmit" value="Submit">
</form>

</body>
</html>

I get that the Submit button sends the equivalent of python dictionary (associative array?) to the next page. Then the command $_POST['formSubmit'] pulls up the value.

But where is the value in between when I hit the submit button to when the page loads. In other words, after the sumbit button, what actually happens? Clearly, the page must create this associative array somewhere and pass it somehow but I'm not sure how it does it.

The idea with a get seems more clear. The URL is appended so the PHP engine can read the URL string and find the values of all the variables (I'm assuming that's what happens yes)?

Thank you for your help!

  • 写回答

1条回答 默认 最新

  • dongpin3794 2014-02-15 02:57
    关注

    HTTP GET passes all its data via the url in a query string:

    http://example.com/script.php?key=value
                                  ^^^^^^^^^^---GET query parameters
    

    This could've been accomplished via:

    <form method="GET" action="http://example.com/script.php"
       <input type="hidden" name="key" value="value" />
       <input type="submit" />
    </form>
    

    This URL becomes part of the HTTP request's headers:

    GET /script.php?key=value HTTP/1.1
    Host: example.com
    Header1: value1
    Header2: value2
    Cookie: something=whatever
    

    A GET request has no 'body' section - the entirety of the request is contained in the URL has the rest of the request's headers.

    HTTP POST can also have query parameters if need be, but generally anything submitted via POST operation sends the data in the HTTP requests "body" section:

    POST /script.php?key=value HTTP/1.1
    Host: example.com
    Header1: value1
    Header2: value2
    Cookie: something=whatever
    
    foo=bar
    

    And this would've been generated by the following form:

    <form method="POST" action="/script.php?key=value" />
       <input type="hidden" name="foo" value="bar" />
       <input type="submit" />
    </form>
    

    Note the blank line between the headers and the key foo. That line is how the webserver knows when the the headers are done and the body begins.

    For this particular sample POSt request, you'd end up with:

    $_GET['key'] = 'value'; // from the requested URL
    $_POST['foo'] = 'bar'; // from the request body
    

    There's also $_REQUEST in PHP, which is an internal PHP-only construct that combines various sources, including $_GET and $_POST into a single monolithic data structure, but its data comes from the exactly same sources.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同