Forum Moderators: coopster

Message Too Old, No Replies

Tip for getting all POST VARS

         

IntegrityWebDev

1:17 pm on Oct 12, 2010 (gmt 0)

10+ Year Member



Thought I'd throw this out there but I'm am more than open to any suggestions on other ways to do this.

Sometimes I will create a form with a LOT of variables in it and I need to get those variable values back out of $_POST, but I may have 50+ variables on a form. Here's what I do.

1) Create my form and add a submit button.
2) Add this at the very top of my page before any other code.
<?php
foreach ($_POST as $var => $value) {
echo "\$";
echo $var;
echo " = \$";
echo "_POST['";
echo $var;
echo "'];<br />";
}
?>

3) Click SUBMIT
4) All my variables show up right at the top of my page.
5) Copy what shows up right on the screen and paste it into my PHP page (replace the code above with your new code).
6) Voila...you just filled in all of your POST vars.

Now...feel free to offer better solutions. I am definitely not the expert here and if there is a better way to do it, I want to know for sure. :-)

Thanks,
Chris

coopster

1:23 pm on Oct 12, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Check out var_export [php.net] Chris. I think you will find it helpful and handy.

Matthew1980

1:40 pm on Oct 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

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

Using this sort of thing on the receiver script will more or less do the same thing. I swear by this, stops spelling errors..

Cheers,
MRb

whoisgregg

1:48 pm on Oct 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think extract [php.net] would also do the trick, plus it checks for collisions so that you don't accidentally overwrite an existing variable.

Because of the potential for people to add their own post variables (like error=false), it's especially important to initialize all your variables.

IntegrityWebDev

2:32 pm on Oct 12, 2010 (gmt 0)

10+ Year Member



I knew if I posted it, I would get some better answers. :-)

rocknbil

3:11 pm on Oct 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Somewhere in your config,

$my_debug = 1;
// 0 or null for off, named so it doesn't conflict
// with other module/class "$debug" variables.
// If used in functions will have to do this

function some_function () {
global $my_debug;
}

or

define('LOCAL_DEBUG','1');
// Named so it doesn't conflict with other
// module/class "DEBUG" constants

then, call it from anywhere as a comment.

echo debug_output();


function debug_output() {
global $my_debug;
// or
// $my_debug = LOCAL_DEBUG;
$debug_str=null;
//
if ($my_debug==1) {
// Use it for either. :-)
if ($_POST) { $debug_str .= "Method is post.<br>\n"; }
else if ($_GET) { $debug_str .= "Method is get.<br>"; }
else { $debug_str .= "No input method is requested, odd.<br>\n"; }
foreach ($_REQUEST as $key => $value) {
$debug_str . "key: $key value: $value<br>\n";
}
}
if ($debug_str) { $debug_str = "<!-- $debug_str -->"; }
return $debug_str;
}


You can have "echo debug_output();" in hundreds of places throughout your program and just leave it in as you develop, even in live apps. Control it only by the single variable/constant turning it on or off. If off, it returns a null, no effect on live apps.