Can I POST and GET in the same PHP page

I wanted to know if GET and POST are possible on the same php page, e.g.

I want to send data:

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

So first get

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

and POST - from $thexml = XML to

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

and then have access to both in some code, for example (example)

 // Example Code ============================ if($_GET["task"] == "dosomething"){ $mynewxml = $_POST["$thexml"]; } //========================================== 
+6
source share
7 answers

Technically not, you cannot POST and GET at the same time. These are two different verbs, and you can only do this at the time of your request.

However, you will find that if you perform POST and include the parameters in the URL, for example yourscript.php?param1=somevalue¶m2=somevalue , then both $_GET and $_POST will be populated with the appropriate data.

It would be wise to read how HTTP works. http://www.jmarshall.com/easy/http/

You should consider whether this is really a good system design on your part. GET intended for requests that do not modify data on the server. A POST can change data. Although both options can be implemented for this, it is better to follow this general practice. You never know what a proxy or other program does up the line otherwise.

+18
source

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> 
+6
source

Here is how I do it ....

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

Yes, you can. Be sure to use $_GET for get and $_POST . There is also $_REQUEST , which combines two in one array. Using this is not recommended.

+3
source

Of course. Pretty easy:

 <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { ... handle form submission here ... } ?> <html> <body> <form action="thisscript.php" method="post"> ... form here ... </form> 
+2
source

You cannot use both methods from the client (two different requests) and see all the parameters with 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.

If you need to correlate data from several different queries (for any reason), you need to independently store and manage these intermediate data.

0
source

Yes, my young Padawan. It is as simple as changing the post attribute on the 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. When sending, you get access to the HTTP Request Post / GET data stored in the corresponding variables.

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

Source: https://habr.com/ru/post/892500/


All Articles