Can I POST and GET to the same PHP page

2019-06-20 18:13发布

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

标签: php xml post get
7条回答
混吃等死
2楼-- · 2019-06-20 18:50

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.

查看更多
相关推荐>>
3楼-- · 2019-06-20 18:50

This is how I do it....

if (isset($_POST['somevar'])) { 
    $somevar = $_POST['somevar']; } 
    else { 
    $somevar = $_GET['somevar']; 
}
查看更多
男人必须洒脱
4楼-- · 2019-06-20 18:53

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']
查看更多
ゆ 、 Hurt°
5楼-- · 2019-06-20 18:57

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.

查看更多
贼婆χ
6楼-- · 2019-06-20 19:05

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>
查看更多
一夜七次
7楼-- · 2019-06-20 19:07

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.

查看更多
登录 后发表回答