php 在php中同时发布和获取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2749406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:32:37  来源:igfitidea点击:

Post and get at the same time in php

phpmysql

提问by user225269

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 access the mysql database in relation to that input. It looks something like this:

你对我的问题有什么建议吗。我需要同时使用 get 和 post。获取因为我需要输出用户输入的内容。并发布,因为我需要访问与该输入相关的 mysql 数据库。它看起来像这样:

<form name="x" method="get" action="x.php">
<input name="year" type="text">

<select name="general" id="general">
        <font size="3">
        <option value="YEAR">Year</option>

</form>

This will output the contents of mysql depending on what the user will check:

这将根据用户将检查的内容输出 mysql 的内容:

<form name="y" method="post" action"y.php">
<input name="fname" type="checkbox">
</form>

And the form action of those two combined will look something like this:

这两个组合的表单操作将如下所示:

   <?php

               if($_POST['general'] == 'YEAR'){
                   ?>
                   <?php echo $_GET["year"]; ?>
                   <?php
            $result2 = mysql_query("SELECT * FROM student
    WHERE student.YEAR='$syear'");
    ?>
    <table border='1'>
            <tr>

                    <?php if ( $ShowLastName ) { ?><th>LASTNAME</th><?php } ?>
                    <?php if ( $ShowFirstName ) { ?><th>FIRSTNAME</th><?php } ?>
        </tr>

    <?php while ( $row = mysql_fetch_array($result2) ) {
        if (!$result2)  { 

    }
        ?>
            <tr> 
                    <td><?php echo $row['IDNO']?> </td>
                    <td><?php echo $row['YEAR'] ?> </td>
     <?php if ( $ShowLastName ) { echo('<td>'.$row['LASTNAME'].'</td>'); } ?></td>
                    <?php if ( $ShowFirstName ) { echo('<td>'.$row['FIRSTNAME'].'</td>'); } ?>

I really get lots of undefined errors when I do this. What can you recommend that I should do in order to get the value inputted by the user together with the mysql data.

当我这样做时,我真的遇到了很多未定义的错误。你有什么建议我应该怎么做才能得到用户输入的值和mysql数据。

回答by Gordon

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

执行HTTP 请求您只能有一个动词(POST、GET、PUT 等)。但是,你可以这样做

<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.

然后 PHP 也会填充$_GET['foo'],尽管发送的请求是 POST 的。

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.

但是,您的问题似乎更多是您试图一次发送两个表单,针对两个不同的脚本。这在一个请求中是不可能的。

回答by zaf

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

您不能同时执行 GET 和 POST。

Combine the two forms into one.

将两种形式合二为一。

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

例如,将表单组合为一个“发布”表单。在您的代码中,从 $_POST 中提取您需要的任何内容。

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

并且'YEAR' 不等于'Year',您的示例代码也需要工作。

回答by David Morales

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

正如其他答案所说,您不能同时执行 get 和 post 请求。但是如果你想统一你的 PHP 代码以读取通过 get 或 post 请求收到的变量,也许你可以使用$_REQUEST

回答by yuri

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):

POST 和 GET(如 HEAD、FILE、DELETE 等)是 HTTP方法。您的浏览器向服务器发送一个 HTTP 请求,其中一个在请求前面,因此您不能同时发送两种方法(来自网络嗅探器的请求标头示例):

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 GETand POSTis that GET retrieve a response from an urland 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.

GETPOST的最大区别在于,GET 从url检索响应,而 POST 还会向该 url 发送一些内容数据。当您提交表单时,数据将以 enctype 属性定义的标准格式收集并发送到 url,这一次也是如此。

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

此外,url 以标准方式格式化,并且在?后面找到的字符串部分字符称为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 fillsits superglobal arrays with this data to let you know what the user sends ($_SERVER, $_GETand $_POSTand a bunch of others);

当服务器接收到数据时,它会将这些信息传达给 PHP,后者读取 URL 并读取方法、请求的正文(数据)和大量其他内容。最后,它用这些数据填充它的超全局数组,让你知道用户发送了什么($_SERVER$_GET$_POST以及其他一些);

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.

重要通知!此外,如果 PHP 使用 URL 的查询字符串填充 $_GET 超全局变量,并最终使用请求正文中找到的数据填充 $_POST 超全局变量,则$_POST 和 $_GET 与 HTTP 无关

So, if you want fill $_POSTand $_GETin the same time you must send your form in this manner:

因此,如果您想同时填写$_POST$_GET,您必须以这种方式发送表单:

<form method="post" action="http://myurl/index.php?mygetvar=1&mygetvar=2">
    <input name="year" type="text" />
    <imput type="submit" />
</form>

回答by RedYetiCo

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

我还没有测试过,但这是通过表单的操作值发送 GET 变量的可能解决方案......

$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=0but they existed in the $_SERVER['REQUEST_URI'] variable. Hope this helps and good luck!

这并不完全是万无一失的,但是我之前遇到了 header("Location:") 错误的问题,其中包括 $_GET 下服务器未看到的 get 变量,其 URL 为http://website.com/page ?msg=0但它们存在于 $_SERVER['REQUEST_URI'] 变量中。希望这会有所帮助,祝你好运!