How to list all post variables?

  • artcoder
  • Student
  • Student
  • User avatar
  • Posts: 89
  • Loc: United States

Post 3+ Months Ago

When a user posts a form, I can retrieve their values by ...

Code: [ Select ]
$_POST['imagefilename']


But this particular form is dynamically constructed so I do not know all the name attributes of all the inputs fields. I don't even know how many input fields there are.

So there could be an input field with names like 'imagefilename-1', 'imagefilename-2', etc.

Is there a way for me to list all this in a PHP loop?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8413
  • Loc: USA

Post 3+ Months Ago

There is a way to do this in a loop. I actually posted this type of thing a while back when I was helping mindfullsilence... let me retrieve it for you.

It came from this post.


Here, I edited it for you to fit your case.
PHP Code: [ Select ]
<?php
/* Some required variables to configure this script */
 
// Variables not to be listed (blocked variables... like the submit button)
$blocked = array('submit');
 
/* The actual code for listing */
 
// Initiating the list var
$list = null;
 
// Building the list
foreach($_POST as $field_name => $field_value)
   if(!in_array($field_name, $blocked))
      $list .= "<strong>{$field_name}</strong>: {$field_value}<br />\r\n";
 
// Trimming the ends of the list from any unneeded white spaces
$list = trim($list);
 
// Returning the list of variables.
echo $list;
?>
  1. <?php
  2. /* Some required variables to configure this script */
  3.  
  4. // Variables not to be listed (blocked variables... like the submit button)
  5. $blocked = array('submit');
  6.  
  7. /* The actual code for listing */
  8.  
  9. // Initiating the list var
  10. $list = null;
  11.  
  12. // Building the list
  13. foreach($_POST as $field_name => $field_value)
  14.    if(!in_array($field_name, $blocked))
  15.       $list .= "<strong>{$field_name}</strong>: {$field_value}<br />\r\n";
  16.  
  17. // Trimming the ends of the list from any unneeded white spaces
  18. $list = trim($list);
  19.  
  20. // Returning the list of variables.
  21. echo $list;
  22. ?>

Should work... I didn't test it but... it should work perfectly. (I tested the code where it came from and that works, so this should work).

If you are using the files button, then it wouldn't be $_POST it would be $_FILE... and if that doesn't work, then it must be $_FILES... don't really remember.

Good luck.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

Code: [ Select ]
echo '<pre>';
print_r($_POST);
echo '</pre>';
  1. echo '<pre>';
  2. print_r($_POST);
  3. echo '</pre>';
  • gkumar
  • Beginner
  • Beginner
  • gkumar
  • Posts: 39

Post 3+ Months Ago

Hi,
Does Php can give us a way to find the name of all the POST variables
transmitted between two pages ?
I'm looking for a similar way to access those variables names than with
javascript and his document.forms["myform"].elements[0], something like
that...

Actually in my first form i have some dynamic names, and it's quite
difficult to exploit later, in form 2, when you have to rebuild them, i have
to use eval(), that's not so easy, so i'm looking for a smarter/easier way
to do it.

Any ideas ?
Thanks,

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 39 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.