发布并在php中同时获取

时间:2022-10-28 21:50:43

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数据,我建议你做什么?

5 个解决方案

#1


47  

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.

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

#2


4  

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”表单。在您的代码中从$ _POST提取您需要的任何内容。

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

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

#3


3  

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

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

#4


3  

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请求,其中一个请求在请求前面,因此您无法同时发送两个方法(来自Web嗅探器的请求标头示例):

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.

与GET和POST的最大区别在于GET从URL检索响应,POST也将一些内容数据发送到该URL。提交表单时,数据将以enctype属性定义的标准格式收集,并且此次也会发送到网址。

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 fills its superglobal arrays with this data to let you know what the user sends ($_SERVER, $_GET and $_POST and a bunch of others);

当服务器接收数据时,它将这些信息传递给PHP,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 $_POST and $_GET in 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>

#5


2  

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

这不是完全万无一失的,但是我遇到了一个问题,前面有一个标题(“位置:”)错误,其中包括服务器在$ _GET下看不到的变量,网址是http://website.com/page ?msg = 0但它们存在于$ _SERVER ['REQUEST_URI']变量中。希望这有帮助,祝你好运!

#1


47  

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.

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

#2


4  

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”表单。在您的代码中从$ _POST提取您需要的任何内容。

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

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

#3


3  

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

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

#4


3  

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请求,其中一个请求在请求前面,因此您无法同时发送两个方法(来自Web嗅探器的请求标头示例):

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.

与GET和POST的最大区别在于GET从URL检索响应,POST也将一些内容数据发送到该URL。提交表单时,数据将以enctype属性定义的标准格式收集,并且此次也会发送到网址。

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 fills its superglobal arrays with this data to let you know what the user sends ($_SERVER, $_GET and $_POST and a bunch of others);

当服务器接收数据时,它将这些信息传递给PHP,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 $_POST and $_GET in 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>

#5


2  

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

这不是完全万无一失的,但是我遇到了一个问题,前面有一个标题(“位置:”)错误,其中包括服务器在$ _GET下看不到的变量,网址是http://website.com/page ?msg = 0但它们存在于$ _SERVER ['REQUEST_URI']变量中。希望这有帮助,祝你好运!