SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Wizard rbutler's Avatar
    Join Date
    Jul 2003
    Location
    Springfield, MO
    Posts
    1,867
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Collection $_POST values from form

    Is there a way when assigning $_POST collection values from form fields to assign them automatically through a foreach loop? The reason I ask is I have a form which contains 40 + fields and assigning variables manually is going to be a task. So what happens is:

    $_POST['myformfield'] turns into $myformfield

    I thought maybe something like this might work but it looked a little too simple:

    Code PHP:
    foreach($_POST as $key=>value){
        ${$key}=$temp;
    }

    Thanks for any help,
    Ryan Butler

    Midwest Web Design

  2. #2
    SitePoint Evangelist barbara1712's Avatar
    Join Date
    Apr 2007
    Location
    India
    Posts
    509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have not tried,but it may help you
    PHP Code:
    if($_POST['submit'])
    {
        foreach(
    $_POST as $k => $v)
        {
            
    "$".$k=$v;        
        }
             echo 
    $myformfield;

    Barbara

  3. #3
    SitePoint Wizard rbutler's Avatar
    Join Date
    Jul 2003
    Location
    Springfield, MO
    Posts
    1,867
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply. That looks the same as what I posted, so perhaps this will work.
    Ryan Butler

    Midwest Web Design

  4. #4
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    STOP! this is a ridiculously bad idea. anyone could then send POST data to your web page and overwrite a variable you otherwise don't want to them to.

    why do you feel the need to copy that entire array in to other variables?

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Ukraine
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you really want to do that, than you can use extract($_POST);

  6. #6
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why not use the variable in the $_POST????
    for example: $name is the same as $_POST['name']

  7. #7
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    doesn't that basically turn register_globals on for the POST variables? Bad!

    I vote for using _POST as well. It's a variable, don't be afraid of the array
    MySQL v5.1.58
    PHP v5.3.6

  8. #8
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by carson1185 View Post
    why not use the variable in the $_POST????
    for example: $name is the same as $_POST['name']
    only if register globals is on, see register globals 101
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  9. #9
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think he means in terms of how easy it is to use. In fact I find it easier to use $_POST when talking about form validations, etc..
    MySQL v5.1.58
    PHP v5.3.6

  10. #10
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Learn from this。

  11. #11
    SitePoint Wizard rbutler's Avatar
    Join Date
    Jul 2003
    Location
    Springfield, MO
    Posts
    1,867
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think there's a bit of misunderstanding. For instance, I have 40+ form fields, to me doing it a typical way:

    Code PHP:
    $myvariable=$_POST['myformfield'];

    Is extremely tedious for multiple form fields. I don't see why this is such a bad idea? I'm validating the form fields with JS and then regular expressions, so the chances of bad input are slim. I actually couldn't get this to work so I'll probably have to back to the above method. Unless someone has a proven method that will work.
    Ryan Butler

    Midwest Web Design

  12. #12
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The point that we are trying to emphasize to you is that you are wasting 40+ variables Why can't you use $_POST directly?

    Also, don't ever trust JS validation. JavaScript can easily be turned off and headers can be manipulated to submit whatever the person wants.
    MySQL v5.1.58
    PHP v5.3.6

  13. #13
    SitePoint Wizard rbutler's Avatar
    Join Date
    Jul 2003
    Location
    Springfield, MO
    Posts
    1,867
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The point that we are trying to emphasize to you is that you are wasting 40+ variables Why can't you use $_POST directly?
    I can, I typically don't do that. Guess a matter of personal preference.

    Also, don't ever trust JS validation. JavaScript can easily be turned off and headers can be manipulated to submit whatever the person wants.
    Re-read my post about server-side validation. I'm fully aware of the JS issue.
    Ryan Butler

    Midwest Web Design


Bookmarks

Posting Permissions

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