Can I POST and GET to the same PHP page

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-03 04:49:07

问题


I wanted to know if it is possible to GET and POST on the same php page, for example

I want to send data to:

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

So first the GET

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

and POST some $thexml = XML to

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"];
    }
//==========================================

回答1:


Technically no, you cannot POST and GET at the same time. 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.

It would be wise to read up on how HTTP actually works. 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. A POST can change data. 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.




回答2:


Yes, you can do this by including the $_GET parameters as part of the form action:

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



回答3:


This is how I do it....

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



回答4:


Yes you can. Make sure to use $_GET for get and $_POST. There is also $_REQUEST which combines the two in one array. Using this is discouraged.




回答5:


Sure. Quite easy:

<?php

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

?>

<html>

<body>

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



回答6:


You can't use both methods from the client (two different requests) and see all parameters in the same execution of your PHP script. You need to choose POST or GET.

You can use both GET and POST data coming from the same request, as others signaled.

If you need to collate data from several different request (for any reason) you need to store and manage those intermediate data yourself.




回答7:


Yes, my young Padawan. It is as simple as changing the post attribute in a form.

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

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


来源:https://stackoverflow.com/questions/6651696/can-i-post-and-get-to-the-same-php-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!