简体   繁体   English

我可以 POST 和 GET 到同一个 PHP 页面吗

[英]Can I POST and GET to the same PHP page

I wanted to know if it is possible to GET and POST on the same php page, for example例如,我想知道是否可以在同一个 php 页面上进行 GET 和 POST

I want to send data to:我想将数据发送到:

http://www.example.com/my.php

So first the GET所以首先是 GET

http://www.example.com/my.php?task=dosomething

and POST some $thexml = XML to并发布一些$thexml = XML

http://www.example.com/my.php?task=dosomething

and then be able to access both in some code like (example)然后能够在一些代码中访问两者(例如)

// Example Code ============================

    if($_GET["task"] == "dosomething"){
      $mynewxml = $_POST["$thexml"];
    }
//==========================================

Technically no, you cannot POST and GET at the same time.从技术上讲,不能,您不能同时POSTGET They are two different verbs, and you only get to make one during your request.它们是两个不同的动词,您只能在请求期间使用一个。

However, you will find that if you do a POST and include parameters in the URL, such as yourscript.php?param1=somevalue&param2=somevalue , then both $_GET and $_POST will be populated with their respective data.但是,您会发现,如果您执行POST并在 URL 中包含参数,例如yourscript.php?param1=somevalue&param2=somevalue ,则$_GET$_POST都将填充各自的数据。

It would be wise to read up on how HTTP actually works.阅读 HTTP 的实际工作原理是明智的。 http://www.jmarshall.com/easy/http/ http://www.jmarshall.com/easy/http/

You should consider whether or not this is good system design on your part.你应该考虑这对你来说是否是好的系统设计。 A GET is supposed to be for requests that don't change data on the server. GET应该用于不更改服务器上数据的请求。 A POST can change data. POST可以更改数据。 Even though both can be implemented to do either, it is best to follow this common practice.尽管两者都可以实现,但最好遵循这种常见做法。 You never know what some proxy or other program up the line will do with it otherwise.你永远不知道一些代理或其他程序会用它做什么。

Yes, you can do this by including the $_GET parameters as part of the form action:是的,您可以通过在表单操作中包含$_GET参数来做到这一点:

<form method='post' action='handler.php?task=dosomething'>
   ...
</form>

This is how I do it....这就是我的做法......

if (isset($_POST['somevar'])) { 
    $somevar = $_POST['somevar']; } 
    else { 
    $somevar = $_GET['somevar']; 
}

Yes you can.是的你可以。 Make sure to use $_GET for get and $_POST .确保将$_GET用于 get 和$_POST There is also $_REQUEST which combines the two in one array.还有$_REQUEST将两者结合在一个数组中。 Using this is discouraged.不鼓励使用它。

Sure.当然。 Quite easy:很容易:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  ... handle form submission here ...
}

?>

<html>

<body>

<form action="thisscript.php" method="post">
... form here ...
</form>

You can't use both methods from the client (two different requests) and see all parameters in the same execution of your PHP script.您不能从客户端使用这两种方法(两个不同的请求)并在 PHP 脚本的同一执行中查看所有参数。 You need to choose POST or GET.您需要选择 POST 或 GET。

You can use both GET and POST data coming from the same request, as others signaled.正如其他人所暗示的那样,您可以使用来自同一请求的 GET 和 POST 数据。

If you need to collate data from several different request (for any reason) you need to store and manage those intermediate data yourself.如果您需要整理来自多个不同请求的数据(出于任何原因),您需要自己存储和管理这些中间数据。

Yes, my young Padawan.是的,我年轻的学徒。 It is as simple as changing the post attribute in a form.就像更改表单中的 post 属性一样简单。

<form method="post"....
<input type="text" name="some_name"...

or或者

<form method="get"....
<input type="text" name="some_name"...

And adding a submit button.并添加提交按钮。 Upon submitting, you access the HTTP Request Post/GET data stored in their respective variables.提交后,您可以访问存储在各自变量中的 HTTP 请求发布/获取数据。

$_POST['some_name'] or $_GET['some_name']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM