ilhan
ilhan

Reputation: 8995

How to grab all variables in a post (PHP)

How to grab all variables in a post (PHP)? I don't want to deal with $_POST['var1']; $_POST['var2']; $_POST['var3']; ... I want to echo all of them in one shot.

Upvotes: 12

Views: 32545

Answers (5)

Yasir
Yasir

Reputation: 1

Use this function in a loop. extract ( $_GET );

Upvotes: -3

Pacific Voyager
Pacific Voyager

Reputation: 79

If you want the POST values as variables in your script, you can use the extract function, e.g.

extract ( $_GET );

or

extract ( $_GET, EXTR_IF_EXISTS );

There are several flags you can use (check the manual); this one restricts extraction to variables already defined.

You can also use the import_request_variables function.

Cheers

Jeff

Upvotes: 5

CaseySoftware
CaseySoftware

Reputation: 3125

If you really just want to print them, you could do something like:

print_r($_POST);

Alternatively, you could interact with them individually doing something like:

foreach ($_POST as $key => $value) {
    //do something
    echo $key . ' has the value of ' . $value;
}

but whatever you do.. please filter the input. SQL Injection gives everyone sleepless nights.

Upvotes: 31

Russell Dias
Russell Dias

Reputation: 73372

echo '<pre>'; 
print_r($_POST); 
echo '</pre>';

Upvotes: 3

Fernando Briano
Fernando Briano

Reputation: 7768

Use:

var_dump($_POST);

Upvotes: 4

Related Questions