Can SESSION do this?

Discussion in 'PHP' started by LongHaul, Jan 2, 2007.

  1. #1
    This seems like an easy thing to do, but the php.net page about sessions rambles on and on and I don't get it :eek:

    I want a webpage called step1.php. On this page, users see a list and can check as many checkboxes as they want to select items.

    Then they click "OK" and go to step2.php. The items they checked on step1.php would be passed on. (I'm thinking, for my purposes, it would be fine to put the checkbox names in an array, and THAT would be passed on to step2.php.)

    The Point: I don't understand the part about php session stuff coming BEFORE all the html. At the top of step1.php, I don't yet know what data to store! The user hasn't made his choices yet. How could you EVER be able to store variables at the beginning of a web page??

    As an aside, I'd have to figure out how to store the checkbox names in an array too.

    I don't want to pass the checkbox values in the url because there could be dozens and it would be so unwieldy. I'd rather do this by temporarily storing the array with a session id. Losing this data when the user closes his browser is fine.

    Can someone point me in the right direction?? Thanks! I'll go try to study the php.net page again. *sigh*
     
    LongHaul, Jan 2, 2007 IP
  2. LongHaul

    LongHaul Peon

    Messages:
    670
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    OK I got it working, without sessions! It is actually very easy, not sure why I thought I needed to create a session and all that.

    Here's what I did:

    1. Passed all the checkboxes that were checked as method="post" with a form submit button. (No work on my part, this is automatic). With post, the data is not included in the URL.

    2. On step2.php, I defined an array and set it to equal $GLOBALS[_REQUEST]. This superglobal variable includes the checkbox names and values, as well as some other junk on the end I don't want.

    3. I used a simple string comparison to strip out the unwanted junk, and I am left with an array of the values the user selected on step1.php!

    Here is the code:
    <?php
    		
    		$postdata=array();
    		$postdata=$GLOBALS[_REQUEST];
    		
    		$chosenboxes=array();
    		foreach($postdata as $dataunit) {
    			if (substr($dataunit, 0, 2) == "QQ") { //On step1.php, I'd given each checkbox the SAME name and value, all of which begin with QQ, which makes it easier to identify the array values to keep on step2.php
    				$chosenboxes[] = $dataunit;
    			}
    		}
    PHP:
    So the URLs look clean, the data is passed, and everything seems to work!
     
    LongHaul, Jan 2, 2007 IP
  3. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Good to hear you sorted it out, but just wanted to refer to:

    "I don't understand the part about php session stuff coming BEFORE all the html. At the top of step1.php, I don't yet know what data to store!"

    for a second. Just so you know, you don't have to know what the values are at the time, you just need to start the session with the session_start function... you can set the session values later on.

    The session_start function is needed at the start so that it can issue the user with a cookie which is then used to track values on the server. That is to say, the only thing that needs to be done at the start is the issuing of the session ID. Values can be assigned to the session whenever you like as those values are never sent to the user.

    Hopefully that clears things up a bit :)
     
    TwistMyArm, Jan 3, 2007 IP
    streety likes this.
  4. LongHaul

    LongHaul Peon

    Messages:
    670
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ah! I'm glad you posted this; even though I didn't end up using sessions above, I probably will in the near future, I was hoping someone would still address my confusion about them :)

    Thanks, that makes a lot more sense. All the examples in online tutorials I saw had EVERYthing at the beginning, session starts and passing variables; I guess the aim was to make it easier to understand but it didn't work on me ;) I'll play around with it more now.

    Thanks!
     
    LongHaul, Jan 3, 2007 IP