Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 7 of 7

Thread: pass an array from one page to another using forms.

  1. #1

    pass an array from one page to another using forms.

    My upload script for my portfolio entry on my website is in 3 parts.
    Part 1 information: such as name client, bio ect..
    Part 2 Thumbnail.
    Part 3 Large/flash file.

    I have one database that holds categories so it can be accessed by both the blog and portfolio database. The blog database works perfectly with it because its only one step to add a blog.

    I allow the user to select (via checkboxes) 1 or more categories for their entry, with the blog page that array is used right away and it works great with a foreach loop to add the proper information to the blog_cat_association table.

    However as all the information for the portfolio entry is added on the last page that array variable has to be transferred from one page to another and THEN used in the foreach loop.

    By the time it gets to the last page the array only holds one number "0".. when it should be holding more than one number each referencing a category >.<


    I don't want to have to use sessions to transfer the data because i'm already using forms and i feel it would be a waste to use both $_POST and $_SESSION globals for same information.

    NOTE: The forms on my page are made by PHP code, i condensed them down to standard html so they would be eaiser for you to see what i am doing.

    here is my code one page 1:
    PHP Code:
    <form>
    <
    input name="categories[]" type="checkbox" value="1" />
    <
    input name="categories[]" type="checkbox" value="2" />
    <
    input name="categories[]" type="checkbox" value="3" />
    </
    form
    that form is submitted and sent to page 2 and i retrieve the information like this:
    PHP Code:
    (empty($_POST['categories'])) {
        
    $errors[] = "<p>You must select at least 1 category.</p>";
    } else {
        
    $categories $_POST['categories'];

    and on page 2 i again SEND the information to page 3 like this:
    PHP Code:
    <form>
    <
    input type="hidden" name="categories[]" value="$categories/>
    </
    form
    and finally i retrieve the information on page 3 like this:
    PHP Code:
    if (empty($_POST['categories'])) {
        
    $errors[] = "<p>You must select at least 1 category.</p>";
    } else {
        
    $categories $_POST['categories'];

    sadly i get an array that only holds one value.. and its '0'.


    any idea what i'm doing wrong?
    Last edited by birdwing; July 24th, 2009 at 10:08 PM.

    Twitter / The Human Conditions

    biznuge: "that doesn't grammatical sense..."

  2. #2
    Each time you submit the form, the $_POST gets new values according to the last submitted form... there is no way to access earlyer submitted forms with $_POST...

    a great solution would be using the $_SESSION, in wich you can save values for the user's session, the session will hold data for you untill the user closes his browser. Its really easy:

    On your 2nd page (after the first submit, do not forget session_start() at the top)
    PHP Code:
    <?php
        session_start
    ();
        if (empty(
    $_POST['categories'])) {
            
    $errors[] = "<p>You must select at least 1 category.</p>";
        } else {
            
    $_SESSION['categories'] = $_POST['categories'];
        }  
    ?>
    after that, every page with session_start(); can access all the variables within the $_SESSION... so you can remove the hidden field on page 2, and go straight to page 3 and change the code to something simular like this:

    PHP Code:
    <?php
        session_start
    ();
        if (empty(
    $_SESSION['categories'])) {
            
    $errors[] = "<p>You must select at least 1 category.</p>";
        } else {
            
    $categories $_SESSION['categories'];
        }
    ?>

    You can never underestimate the stupidity of the general public.

  3. #3
    I realize that the $_POST variables get reset.. which is why after they were sent from the first page to the 2nd.. i placed them inside a hidden form on the 2nd page and sent them again to the 3rd page...

    I was hoping to avoid sessions because I don't need or use them anywhere else in my entire web application and wanted to remain consistent...


    *sigh*.. there has to be some reason why it wont transfer...

    its..

    $_POST['categories'] saved to $categories - put into the hidden field named categories[] then transfered to page2.

    $_POST['categories'] saved again to $categories - put into another hidden field named categories[] then transfered to page 3.

    shouldn't $_POST['categories'] be the same on all 3 pages since its being transferred each time?

    What am I doing wrong?


    Even if there is an easier way to do this (sessions) I am still curious as to why this won't work.. it means I don't fully understand how arrays transfer...


    simpler question.

    How do I take array1
    and make a new array variable containing all the same data as array1?

    wouldn't it be

    array2 = array1? or is there more syntax that i'm missing?



    because actually its a multidimensional array that i'm trying to transfer...

    its an array inside the $_POST array...

    how would i save $_POST['categories'] to an array without it being multidimensional..

    and then how would i transfer that new array to another page using the post method... thats what i need to know..... >.<


    *breeeeaaaath*



    sry if i seem stubborn... but this is getting on my nerves... I need to understand what I am doing wrong if i am to better understand variables....
    Last edited by birdwing; July 24th, 2009 at 10:47 PM.

    Twitter / The Human Conditions

    biznuge: "that doesn't grammatical sense..."

  4. #4
    Ah... i thought you needed a straight solution... if you really do not want to work with sessions, this should be the solution:

    If you look at the result of:
    PHP Code:
    <form>
        <
    input type="hidden" name="categories[]" value="$categories/>
    </
    form
    The value of catogories[0] will be set to "Array" (a string, not the actual array), in order to pass all the data, you will need to put every value in a hidden field...

    PHP Code:
    <?php
        
    if($_POST['categories']){
            foreach(
    $_POST['categories'] as $key => $value){
                echo 
    '<input type="hidden" name="categories['.$key.']" value="'.$value.'"';
            }
        }
    ?>
    This will retain the same order and values for your categories array earlyer created.

    Let me know if that helped

    You can never underestimate the stupidity of the general public.

  5. #5
    wow.. really that seems stupid to me >.<

    why can't i just copy one array into another.. *sigh* again PHP beats me with its sheer stupidity..

    in my Opinion you should be able to copy 1 array into another like

    array1 = array2...

    wow.. sessions it is then -.- thanks for the help though

    Twitter / The Human Conditions

    biznuge: "that doesn't grammatical sense..."

  6. #6
    Quote Originally Posted by birdwing View Post
    wow.. really that seems stupid to me >.<
    in my Opinion you should be able to copy 1 array into another like

    array1 = array2...
    If it was php only, then yes, you could... but this is a mixture of 2 different languages... php and html, you're trying to fit an array (multivalue thingy) into an input field (single value)... it just wont work...

    You may have noticed you're doing it the other way... array from html to php... this is an feature of php, that converts any variables_like_this[] to an array.

    You can never underestimate the stupidity of the general public.

  7. #7
    interesting... i could repopulate it with a loop each time....

    but it works with sessions *shrugs* oh well guess people will just have to deal with having sessions for one section of the app


    again thanks for the help. I think i understand it now.

    Twitter / The Human Conditions

    biznuge: "that doesn't grammatical sense..."

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012