Austin
Austin

Reputation: 1627

PHP post all form inputs without creating variables for each

Usually when I use PHP I create a variable and post statement for each form input like so:

   $myVar1 = $_POST["formItem1"];
   $myVar2 = $_POST["formItem2"];
   ect......

I know if i use:

  echo $_POST;

I can get all the key and values from the form inputs but I don't know how to use them in a script.

So I guess I have 2 questions:

  1. How do I quickly post all form inputs without creating a variable for each input?

  2. How do I use a posted in a script without having it assigned to a specific variable?

Upvotes: 1

Views: 11293

Answers (7)

Dave
Dave

Reputation: 3658

Define a function that allows you to access a specific $_POST item by name (key):

function get_post_value($name, $default)
{
    if ( isset($_POST[$name]) ) {
        return $_POST[$name];
    } else if ( $default ) {
        return $default;    
    }
    return null;
}

$default allows you to pass a value that can be used as a fallback if the key you specify isn't present in the $_POST array.

Now you can reference $_POST items without assigning them to a variable, and without worrying if they are set. For example:

if ( get_post_value('user-login-submit', false) ) {
    // attempt to log in user
}

Upvotes: 0

Mohamed Amine
Mohamed Amine

Reputation: 2304

You can use extract($_POST), it will create variables for you.

For example, you can have for the code you posted :

<?php

$_POST["formItem1"] = "foo";
extract($_POST);
echo $formItem1; // will display "foo"

?>

EDIT : it's not PHP explode, it's extract.

Upvotes: -2

MonkeyZeus
MonkeyZeus

Reputation: 20737

View everything in $_POST is useful for debugging

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

Access a specific checkbox

HTML

<input type="checkbox" value="something" name="ckbox[]" checked>
<input type="checkbox" value="anotherthing" name="ckbox[]" checked>

PHP

echo $_POST['ckbox'][0]; // something
echo $_POST['ckbox'][1]; // anotherthing

// isolate checkbox array
$ckbox_array = $_POST['ckbox'];

Upvotes: 0

vonUbisch
vonUbisch

Reputation: 1469

Another way to extract (besides the extract function) variables is;

$array = array('foo'); // Which POST variables do you want to get
foreach($array as $key) {
    if(!isset(${$key})) { // Check if variable hasn't been assigned already
        ${$key} = $_POST[$key];
    }
}
echo $foo;

I would not recommend it because it can get quite messy to keep up.

Upvotes: 0

Joe
Joe

Reputation: 2105

for this instance of just quickly checking and testing i typically just use the print_r() function: documentation here

as quoted from the docs:

print_r() displays information about a variable in a way that's readable by humans.

one line easy to toggle on and off (with comments)- no need to use any form of variables

print_r($_POST);

if i need my output nice and readable i like to expand it as follows:

function print_r2($x){
     echo '<pre>';
     print_r($x);
     echo '</pre>';
}

and then you can call with: print_r2($_POST);

this way you get the pre-formatted text block on your html page and can see the line breaks and tabbed spacing provided from the $_POST object printout

Upvotes: 0

Asenar
Asenar

Reputation: 7010

You don't have to assign to a variable. You can use directly $_POST['input_name']

If you want to deal with each sended params, you can use foreach loop:

foreach ($_POST as $key => $val)
{
  echo "$key : $val <br/>";
}

Upvotes: 0

Amal Murali
Amal Murali

Reputation: 76646

To simply echo all the inputs, you can use a foreach loop:

foreach ($_POST as $key => $value) {
    echo $value.'<br/>';
}

If you don't want to store them in variables, use arrays:

$data = array();
foreach ($_POST as $key => $value) {
    $data[] = $value;
}

Now, $data is an array containing all the data in $_POST array. You can use it however you wish.

Upvotes: 6

Related Questions