Skip to content Skip to sidebar Skip to footer

Is There A Way To Find Out Post Variables Without Knowing Their Names?

I have a form that gets its names for checkboxes dynamically. Is there a way to find out the names of the unknown variables? For example: foreach($value as $option){ $

Solution 1:

Use the predefined variable $_POST and loop over :

foreach($_POST as $key => $value)
{
    // $key will be the name
    // $value will be the value of $_POST[$key]
}

Solution 2:

You can loop over the post and get fields dynamically:

foreach($_POSTas$key=>$value) {
    echo"$key: $value\n";
}

You can do the same with $_GET.

Solution 3:

You can name your checkboxes 'checkbox[$key][]' and iterate over $_POST[$key] using foreach

Solution 4:

You can loop through the $_POST variable just like any other array

foreach($_POSTas$key => $value)
    echo"$key is $value";

Solution 5:

There is also a function - get_defined_vars() - that returns an array with all defined variables. Try the code below

$arr = get_defined_vars();
echo"<pre>"; print_r($arr);

Post a Comment for "Is There A Way To Find Out Post Variables Without Knowing Their Names?"