Post and get at the same time in php

前端 未结 6 2462
南方客
南方客 2020-11-27 05:56

Do you have any suggestions with my problem. I need to use get and post at the same time. Get because I need to output what the user has typed. And post because I need to ac

相关标签:
6条回答
  • 2020-11-27 06:09

    POST and GET (as HEAD, FILE, DELETE etc.) are HTTP methods. Your browser send an HTTP request to the server with one of them in front of the request so you cannot sent two method at the same time (an example of the request header from a web sniffer):

    GET / HTTP/1.1[CRLF]
    Host: web-sniffer.net[CRLF]
    Connection: close[CRLF]
    User-Agent: Web-sniffer/1.0.31 (+http://web-sniffer.net/)[CRLF]
    Accept-Encoding: gzip[CRLF]
    Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7[CRLF]
    Cache-Control: no[CRLF]
    Accept-Language: de,en;q=0.7,en-us;q=0.3[CRLF]
    Referer: http://web-sniffer.net/[CRLF]
    

    The big difference from GET and POST is that GET retrieve a response from an url and POST send also some content data to that url. When you submit your form, data is collected in a standard format defined by enctype attribute and sent, also this time, to an url.

    Also the url is formatted in a standard manner and the portion of string found behind the ? character is called QUERY STRING.

    When the server receives data, it communicates these informations to PHP which reads the URL and reads the method, the body (data) of the request and a huge amount of other things. Finally it fills its superglobal arrays with this data to let you know what the user sends ($_SERVER, $_GET and $_POST and a bunch of others);

    Important Notice! Also if PHP fill the $_GET superglobal with the query string of the URL and eventually the $_POST superglobal with the data found in the request body, $_POST and $_GET are not related to HTTP.

    So, if you want fill $_POST and $_GET in the same time you must send your form in this manner:

    <form method="post" action="http://myurl/index.php?mygetvar=1&mygetvar=2">
        <input name="year" type="text" />
        <imput type="submit" />
    </form>
    
    0 讨论(0)
  • 2020-11-27 06:11

    You cannot do a GET and POST at the same time.

    Combine the two forms into one.

    For example combine the forms to one 'post' form. In your code extract whatever you need from $_POST.

    And 'YEAR' does not equal 'Year', your sample code also needs work.

    0 讨论(0)
  • 2020-11-27 06:18

    You can also use a hidden field in the form

    <input type="hidden" id="whatever" name="foo" value="bar">
    

    and use

    $_POST['foo']
    

    to retrieve the value.

    0 讨论(0)
  • 2020-11-27 06:19

    As saig by the other answers, you can't do a get and a post request at the same time. But if you want to unify your PHP code in order to read a variable received through a get or post request, maybe you could use $_REQUEST

    0 讨论(0)
  • 2020-11-27 06:21

    I haven't tested this but it's a possible solution for sending GET variables through a form's action value...

    $request_uri = $_SERVER['REQUEST_URI'];
    $request_uri = str_replace("&", "?", $request_uri);
    $request_args = explode("?", $request_uri);
    foreach($request_args as $key => $val) {
        if(strpos($val, "=") > 0) {
            $nvp_temp = explode("=", $val);
            $_GET[$nvp_temp[0]] = $nvp_temp[1];
        }
    }
    

    It's not completely fool proof, but I ran across an issue with a header("Location:") bug earlier that included get variables not being seen by the server under $_GET with a url such as http://website.com/page?msg=0 but they existed in the $_SERVER['REQUEST_URI'] variable. Hope this helps and good luck!

    0 讨论(0)
  • 2020-11-27 06:24

    You can only have one verb (POST, GET, PUT, ...) when doing an HTTP Request. However, you can do

    <form name="y" method="post" action="y.php?foo=bar">
    

    and then PHP will populate $_GET['foo'] as well, although the sent Request was POST'ed.

    However, your problem seems to be much more that you are trying to send two forms at once, directed at two different scripts. That is impossible within one Request.

    0 讨论(0)
提交回复
热议问题