2

I am trying to make a form in PHP, I used user ID to show data in form by GET, but after submitting form by POST i stored user ID in a hidden field..

While trying this i just became confused with GET, POST and REQUEST.

See this situation

<form action="script.php?id=777" method="post">
     ID: <input type="text" name="id" />
     <input type="submit" value="Send" />
</form>

Let's suppose i enter '888' in text field when this form will be submitted, what value $_REQUEST['id']; should provide?

It will be same in all php versions?

What will happen if I left text field blank?

and what will happen if I change action as action="script.php?id="?

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Aniket Singh
  • 2,464
  • 1
  • 21
  • 32

2 Answers2

3

01

If form is in post method

<form action="script.php" method="post">
     ID: <input type="text" name="id" />
     <input type="submit" value="Send" />
</form>

In script.php, you can get data by using

$id = $_POST['id'];//works
$id = $_REQUEST['id'];//works
$id = $_GET['id'];//Failed

02

If form is in get method

<form action="script.php" method="get">
     ID: <input type="text" name="id" />
     <input type="submit" value="Send" />
</form>

In script.php, you can get data by using

$id = $_GET['id'];//works
$id = $_REQUEST['id'];//works
$id = $_POST['id'];//Failed

You can refer $_REQUEST vs $_GET and $_POST and What's wrong with using $_REQUEST[]?

Community
  • 1
  • 1
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
3

The actual order is determined in the "request_order" setting in the PHP.ini file

; This directive determines which super global data (G,P,C,E & S) should
; be registered into the super global array REQUEST. If so, it also determines
; the order in which that data is registered. The values for this directive are
; specified in the same manner as the variables_order directive, EXCEPT one.
; Leaving this value empty will cause PHP to use the value set in the
; variables_order directive. It does not mean it will leave the super globals
; array REQUEST empty.
; Default Value: None
; Development Value: "GP"
; Production Value: "GP"
; http://php.net/request-order
request_order = "GP"

Usually the default setting is Get then Post. In this case you supply the id parameter as get AND as a post parameter. This means the $_REQUEST is populated with the $_GET first, then $_POST. Meaning $_REQUEST will reflect $_POST.

Ronald Swets
  • 1,669
  • 10
  • 16