Global Array

Discussion in 'PHP' started by adamjblakey, Oct 23, 2009.

  1. #1
    Hi,

    What i am trying to do but dont know how do is create a function which i want to call after i have posted a form. This function will store all POST values in a global array. I have a sequence of about 4 pages all with forms on. I want to call the function each time i post each page so all fields are stored in the array which i can call at the end.

    Any help would be much appreciated.

    Cheers,
    Adam
     
    adamjblakey, Oct 23, 2009 IP
  2. namoffer

    namoffer Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you want to use a feature on many pages I think the best and serve you a cookie
     
    namoffer, Oct 23, 2009 IP
  3. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    use sessions...
    the easiest and best solution...
     
    xenon2010, Oct 23, 2009 IP
  4. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Create an array with all the POST fields you want to store in it and put that array in session. Every time you want to add / modify elements of POST data, take out the array from session, do add / modify and put back the array in session.

    Finally after sequence of all pages, you can do processing on all the POST data at once as per your wish :)
     
    mastermunj, Oct 23, 2009 IP
  5. LOD

    LOD Member

    Messages:
    319
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    35
    #5
    session is the best option for u.. since the data is passed as post..
     
    LOD, Oct 23, 2009 IP
  6. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Thanks for that, i am now going to put the values in an array and then store the array in a session.

    Please can someone help though with putting the data in an array.

    I need each value of the array to be called the name of the post and it to contain the post value.

    I have tried a few things but can't get my head around it.
     
    adamjblakey, Oct 26, 2009 IP
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    $_SESSION['form_name'] = array('name' => $_POST['name_field'], 'phone' => $_POST['phone']);
    PHP:
    (continue with whatever you need from the form like that)
     
    JAY6390, Oct 26, 2009 IP
  8. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #8
    Hi, thanks for that but I was after a foreach solution as I have hundreds of fields.

    Cheers,
    Adam
     
    adamjblakey, Oct 26, 2009 IP
  9. heavydev

    heavydev Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    
    foreach ($_POST as $key=>$value){
       $form_values[$key] = $value;
    }
    $_SESSION['form_values'] = $form_values;
    
    PHP:
    something like this?
     
    heavydev, Oct 26, 2009 IP
  10. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #10
    Hi,

    Thanks for that, makes it a lot easier for me.

    Cheers,
    Adam
     
    adamjblakey, Oct 27, 2009 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    Using foreach() this way makes no sense at all. You're unnecessarily creating a copy of the $_POST array, which already holds the data of hundreds of fields.

    Do this on the first page:
    
    $_SESSION['fields'] = $_POST;
    
    PHP:
    ... and this on the other pages:
    
    $_SESSION['fields'] = array_merge($_SESSION['fields'], $_POST);
    
    PHP:
     
    nico_swd, Oct 27, 2009 IP