0

I am unable to use a form with both POST and GET variables.

like, i want to access $_POST['sub1'] and $_GET['id'] in a php page.

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • `$_REQUEST[]`. For all. – deEr. May 19 '18 at 09:43
  • You can access both, so long as both is sent. But through a form, you can only send one or the other through its method. If you have a form with `method="POST"`, but `action="page.php?id=1"` you can access both the POST-values, and `$_GET['id']`. All POST and GET values can be found in `$_REQUEST` (recommend you stay clear of this one though). – Qirel May 19 '18 at 09:44
  • From the manual: The variables in `$_REQUEST` are provided to the script via the GET, POST, and COOKIE input mechanisms and therefore could be modified by the remote user and cannot be trusted. The presence and order of variables listed in this array is defined according to the PHP variables_order configuration directive. – Qirel May 19 '18 at 09:47
  • why you need this, what's your requirement ? – Niklesh Raut May 19 '18 at 09:49
  • Perhaps you could explain your problem in more detail, and include some code. – Progrock May 19 '18 at 11:06

3 Answers3

1

Use $_REQUEST.

An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.

http://php.net/manual/en/reserved.variables.request.php

steadweb
  • 15,364
  • 3
  • 33
  • 47
1

Use $_REQUEST.
but it's not good recommendation.
read this: Among $_REQUEST, $_GET and $_POST which one is the fastest?

Saeed
  • 118
  • 1
  • 11
0

This works for me:

<?php
$id   = $_GET['id']    ?? null;
$sub1 = $_POST['sub1'] ?? null;
var_dump($id, $sub1);
?>
<form action="?id=foo" method="post">
    <input type="submit" name="sub1" value="bar">
</form>
Progrock
  • 7,373
  • 1
  • 19
  • 25