Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2004
    Location
    Bay Area -USA
    Posts
    1,738

    Lots Of POST variables into an array with foreach?

    So I have this page that has a pretty large amount of check boxes which are all submitted to a php page.

    I have started doing like $category1 = $_POST['category1'];
    but its pretty time consuming.

    I think I have seen a way to do it with foreach so that you can save the variables into an array. Something like

    PHP Code:
    foreach($_POST as $name=>$val){
        
    $cats[$name] = $val;

    Is this possible and if so can you show me? Thanks.
    <<< Please see Forum Guidelines for signature setup. >>>

  2. #2
    Join Date
    May 2001
    Location
    Dayton, Ohio
    Posts
    4,962
    http://us4.php.net/extract

    PHP Code:
    extract($_POSTEXTR_SKIP); 
    -Mat Sumpter
    Director, Product Engagement
    Penton Media

  3. #3
    Join Date
    Jun 2004
    Location
    Bay Area -USA
    Posts
    1,738
    Wow Thanks! worked like a charm!
    <<< Please see Forum Guidelines for signature setup. >>>

  4. #4
    Thanks. That does work like a charm

  5. #5
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    1,082
    Using extract is almost as dangerous as enabling register_globals. It will pollute your namespace and can trample on existing configuration variables etc. I'd advise that you do something more constrained so there's less chance of introducing exploits into your code. Try this:
    PHP Code:
    $post_vars = array('category1''category2''description');
    foreach (
    $post_vars as $k)
    {
       $
    $k = (isset($_POST[$k]) ? $_POST[$k] : null);


  6. #6
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    1,082

  7. #7
    Join Date
    Jun 2004
    Location
    Bay Area -USA
    Posts
    1,738
    This is kinda related. If you dont know what the variable names are (for example from a script that you cannot find the name of the variable being posted, is there a way you can be shown what variables are being posted?
    <<< Please see Forum Guidelines for signature setup. >>>

  8. #8
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    Sure there is :

    PHP Code:
    $variables_being_posted array_keys($_POST); 
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  9. #9
    Join Date
    Jun 2004
    Location
    Bay Area -USA
    Posts
    1,738
    Thanks a lot.
    <<< Please see Forum Guidelines for signature setup. >>>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •