SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 46
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Keeping hidden data across pages

    I guess I don't understand "Sessions" very well, so sure could use some help here.

    1. I'm starting with a form1.php page putting: session_start(); at the top There is a lengthy 'hidden' field string with stuff to get processed down the line.

    2. Then we go to page form2.php for some additional Form stuff and another 'hidden' field to get added.

    3. Then to form3.php for some final processing. This is where I am now getting a bunch of 'undefined' errors. When I went directly from form1.php to (form3.php) I had no problems.

    I thought once I started a 'Session' that this would keep things intact across various pages? Is there something else I am missing?

    Thanks for any help.

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    instead of
    PHP Code:
    <input type="hidden" value="<? echo $hidden_val?>">
    try doing
    PHP Code:
    $_SESSION['hidden1'] = $hidden_val
    The point of the session is to NOT put the information into the form.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2003
    Location
    Watford, UK
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi RocketMan,

    Do you have the session_start() call at the top of all three pages?

    Cheers,

    Jon

  4. #4
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Moved to PHP. Not an Advanced PHP issue.
    Aaron Brazell
    Technosailor



  5. #5
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hei.

    You're mixing up a bit the difference between session and hidden fields. I'll give you two different ways. One using session and one hidden fields. (You can of course combine those two, but I leave that out here.)

    Hidden fields:
    PHP Code:
    <?php
    // form1.php
    echo '<form action="form2.php" method="post" />';
    // Id is hidden from the beginning
    echo '<input type="hidden" name="id" value="2" />'
    // Other fields the user is supposed to fill in
    echo 'Name: <input type="input" name="name" /><br />'
    echo 
    'Phone: <input type="input" name="phone" /><br />'
    echo 
    '<input type="submit" name="submit" value="Submit" />';
    echo 
    '</form>';


    // form2.php
    echo '<form action="form3.php" method="post" />';
    // Id, name and phone are hidden through hidden fields
    echo '<input type="hidden" name="id" value="' $_POST['id']. '" />'
    echo 
    '<input type="hidden" name="name" value="' $_POST['name']. '" />'
    echo 
    '<input type="hidden" name="phone" value="' $_POST['phone']. '" />'
    // Other fields the user is supposed to fill in
    echo 'Address: <input type="input" name="address" /><br />'
    echo 
    'Region: <input type="input" name="region" /><br />'
    echo 
    '<input type="submit" name="submit" value="Submit" />';
    echo 
    '</form>';


    // form3.php
    // Printing out all the fields
    echo 'Id:' $_POST['id'] . '<br />'
    echo 'Name:' $_POST['name'] . '<br />'
    echo 'Phone:' $_POST['phone'] . '<br />'
    echo 'Address:' $_POST['address'] . '<br />'
    echo 'Region:' $_POST['region'] . '<br />'
    ?>
    Or using session.
    PHP Code:
    <?php
    // form1.php
    session_start();
    // Id is hidden from the beginning in the session
    $_SESSION['id'] = 2;
    // Other fields the user is supposed to fill in
    echo '<form action="form2.php" method="post" />';
    echo 
    'Name: <input type="input" name="name" /><br />'
    echo 
    'Phone: <input type="input" name="phone" /><br />'
    echo 
    '<input type="submit" name="submit" value="Submit" />';
    echo 
    '</form>';


    // form2.php
    session_start();
    // Id, name and phone gets hidden in the session
    $_SESSION['name'] = $_POST['name'];
    $_SESSION['phone'] = $_POST['phone'];
    // Other fields the user is supposed to fill in
    echo '<form action="form3.php" method="post" />';
    echo 
    'Address: <input type="input" name="address" /><br />'
    echo 
    'Region: <input type="input" name="region" /><br />'
    echo 
    '<input type="submit" name="submit" value="Submit" />';
    echo 
    '</form>';


    // form3.php
    // Printing out all the fields
    echo 'Id:' $_SESSION['id'] . '<br />'
    echo 'Name:' $_SESSION['name'] . '<br />'
    echo 'Phone:' $_SESSION['phone'] . '<br />'
    echo 'Address:' $_POST['address'] . '<br />'
    echo 'Region:' $_POST['region'] . '<br />'
    ?>
    Hopefully this gives you the answers you're looking for. If not please just let us know.

    I've not tested the code, so there might be some typos and bugs.

    -Helge

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to all who replied.

    I have a specific question for Helge:

    In form1, why is there $_SESSION['id'] = 2 but in form2 and form3 no similar code? I'm not understanding.
    PHP Code:
    // form1.php 
    session_start(); 
    // Id is hidden from the beginning in the session 
    $_SESSION['id'] = 2

    [
    font=Courier New][color=#006600][/color][/font] 


    Umh, my ISP runs with PHP "Register_globals OFF" ... I thought I had to use $_POST or $HTTP_POST_VARS to get the data?

    I should mention that in form1, the 'hidden' string has a value that looks like: <abc^123^blah^blah2> etc., etc. This gets parsed out with a 'list' function with ^ & explode and then $_POST['stuff'] and works great...but ONLY if I go directly from form1 to form3.

    So I don't read the form1 'hidden' field until form3, where I must also read the 'hidden2' field stuff entered in form2. And it is in form3 that I must do some math computations based upon *some* of the parsed 'hidden' string $variables from BOTH form1 and form2.

    Then, final form3 sends the stuff to the Authorize.Net gateway via the SIM method.

    Apparently, I must but session_start(); on each of my 3 form pages, but if all pages are a part of ONE 'session', I'm just not understanding that id=2 part on only one page.

    I really appreciate all the help here, but I'm still somewhat clueless.
    Last edited by RocketMan; Sep 11, 2003 at 17:21.

  7. #7
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I'll try to explain more carefully.
    Quote Originally Posted by RocketMan
    In form1, why is there $_SESSION['id'] = 2 but in form2 and form3 no similar code? I'm not understanding.
    Quote Originally Posted by RocketMan
    Apparently, I must but session_start(); on each of my 3 form pages, but if all pages are a part of ONE 'session', I'm just not understanding that id=2 part on only one page.
    When you have put a variable into a session, you don't need to do that on every page. And you only need session_start() on those pages that you want to do something with the session. Even if you go to a page that doesn't use sessions, the session variables added on other pages are still in the session. Look at these four files
    PHP Code:
    // ***** Page 1 *****
    // Need session_start() because I add a value to the session
    session_start();
    $_SESSION['var'] = 100;
    echo 
    '<a href="page2.php">Page2.php</a>';


    // ***** Page 2 *****
    echo '<a href="page3.php">Page3.php</a>';


    // ***** Page 3 *****
    // Need session_start() because I want to add another value/variable to the session.
    session_start();
    $var2 "I'm adding some text here";
    $_SESSION['var2'] = $var2;
    echo 
    '<a href="page4.php">Page4.php</a>';


    // ***** Page 4 *****
    // Need session_start() because I'm going to echo out whats stored in the session.
    session_start();
    echo 
    'var: ' $_SESSION['var'] . '<br />';  //Will print: 100
    echo 'var2: ' $_SESSION['var2'] . '<br />'//Will print: I'm adding some text here 
    In page 2 you see I don't do anything with the session. But the information in the session is still there. On page 3 you see I add another variable to the session. But I don't have to add the first variable one more time though. And on page 4 I print out both of the session variables.

    Quote Originally Posted by RocketMan
    Umh, my ISP runs with PHP "Register_globals OFF" ... I thought I had to use $_POST or $HTTP_POST_VARS to get the data
    Yes you do. You should always use the superglobals ($_POST, $_GET, $_SESSION etc.) And I also do that in the examples.

    Quote Originally Posted by RocketMan
    I should mention that in form1, the 'hidden' string has a value that looks like: <abc^123^blah^blah2> etc., etc. This gets parsed out with a 'list' function with ^ & explode and then $_POST['stuff'] and works great...but ONLY if I go directly from form1 to form3.

    So I don't read the form1 'hidden' field until form3, where I must also read the 'hidden2' field stuff entered in form2. And it is in form3 that I must do some math computations based upon *some* of the parsed 'hidden' string $variables from BOTH form1 and form2
    The data, that you want to get from page 1 to page 3, needs to be put into the form (in hidden or visble fields) or a session on page 2 to be accessible on page 3. Do you get what I mean?
    First you send data from page 1 to page 2. On page 2 you add the data from page 1 into a session or into the form that's on page 2. That way you will be able to access all data (sent from both page 1 and page 2) on page 3.

    The clue is that a session and a form (with hidden and/or visible fields) are two totally separate things. If I understand your problem correctly you don't have to use a session (but it's convenient though. Less typing ) to get the data sent from page 1 ,through page 2, to page 3. On page 2 you put the data from page 1 into hidden fields in the form instead of into the session. You don't need to add the data into both session and hidden field.

    Sorry I don't manage to explain this very well. But don't hesitate to ask further id anything is unclear.

    -Helge

  8. #8
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Helge
    Hi, I'll try to explain more carefully. images/smilies/smile.gif

    The clue is that a session and a form (with hidden and/or visible fields) are two totally separate things. If I understand your problem correctly you don't have to use a session (but it's convenient though. Less typing images/smilies/wink.gif) to get the data sent from page 1 ,through page 2, to page 3. On page 2 you put the data from page 1 into hidden fields in the form instead of into the session. You don't need to add the data into both session and hidden field.
    -Helge
    Hello again, Helge.

    Thank you very much for taking the time to try and help me understand this thoroughly.

    Regarding the above, I will explain.

    On page1.php, I have the 'hidden' field value="abc^blah^blah" tag.

    So, you are saying that I can go to page2.php, perhaps add a 'hidden2' field, and then finally on page3.php I can read the initial page1.php 'hidden' tag using $_SESSION['fieldname']??? I thought this required $HTTP_POST_VARS, etc.

    I just tried the $_SESSION['fieldname'] with another problem script which is a part of everything. Perhaps you can help me see where my error(s) are? I am testing this in 'modules'.

    The first page works fine by itself with error reporting, etc.
    PHP Code:
    <?php 
    ob_start
    (); 
    session_start();
    error_reporting(2039); // Temporary fix 
    $submit $_POST['submit']; 
    $taxrate $_POST['taxrate'];
    $notaxrate $_POST['notaxrate'];
    // $domain = $_SERVER['SERVER_NAME']; 
    $self $_SERVER['PHP_SELF']; 
    if (isset(
    $submit)) {
    if ((
    $taxrate == "info" ) && (!isset($notaxrate))) {
    echo 
    "Please select a tax area.";
    } elseif ((
    $taxrate != "info" ) && (isset($notaxrate))) {
    echo 
    'Please choose from only one set of options'
    } elseif ((
    $taxrate != "info" ) && (!isset($notaxrate))) {
    $salestax $taxrate;
    header('Location: nextfile.php'); // go to nextfile IF no error
    } elseif (($taxrate == "info" ) && (isset($notaxrate))) {
    $salestax $notaxrate;
    header('Location: nextfile.php'); // go to nextfile IF no error

    // END isset($submit) 
    ?> 
    <FORM METHOD="POST" ACTION="<?=$self?>"> 
    MY STATE (USA) CUSTOMERS ONLY:<br>
    Please Choose SHIP-TO DESTINATION Area<br>
    And Then Click The PROCEED Button Below<br>
    <SELECT NAME="taxrate" size="1">
    <OPTION VALUE="info" SELECTED>--Select SHIP-TO Destination--</OPTION>
    <OPTION VALUE=".0825">My Own County</OPTION>
    <OPTION VALUE=".0725">All OTHER State Counties</OPTION>
    <input type="hidden" name="taxcalc" value="taxrate">
    </SELECT>
    <br><br>
    NON-STATE CUSTOMERS:<br> 
    Please Check This Box And Then PROCEED<br>
    <input name="notaxrate" type="checkbox" value=".00">NON STATE CUSTOMER<br>
    <br>
    <INPUT name="submit" type="submit" value="PROCEED">
    &nbsp;&nbsp;&nbsp; 
    <INPUT name="reset" type="reset" value="Reset Form"> 
    </FORM> 
    <?php 
    ob_end_flush
    (); 
    ?>
    But when I get to the page2 (newfile.php), I can NOT read the $salestax variable from page1.
    PHP Code:
    <?php
    // nextfile.php
    session_start();
    error_reporting(2039); // Temporary fix
    $salestax $_SESSION['salestax'];
    echo 
    "Sales Tax: "."$salestax";
    ?>
    So, in newfile.php, the $salestax value(s) to display should be as follows for the options selected in page1:

    IF: (List) My Own County -> $salestax = .0825
    IF: (List) ALL OTHER State Counties -> $salestax = .0725
    IF: (Checkbox) NON STATE CUSTOMER -> $salestax = .00
    Actually, in reality, the above will be page2 and page3 because page1 will contain the unique value="abc^blah^blah" string that ultimately gets parsed & utilized in the final process.

    Thank you!
    Last edited by RocketMan; Sep 12, 2003 at 01:06.

  9. #9
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Helge:

    RE:
    PHP Code:
    // ***** Page 1 ***** 
    // Need session_start() because I add a value to the session 
    session_start(); 
    $_SESSION['var'] = 100
    echo 
    '<a href="page2.php">Page2.php</a>'
    Exactly why do you have to "add a value to the session" ???

    Thanks.

  10. #10
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Helge:

    OK...I think I am now understanding about the 'hidden' fields across pages.

    Page1:
    'hidden' field "blah"

    Page2:
    'hidden' field "blah" MUST be added again as:
    PHP Code:
    <INPUT TYPE="hidden" name="item" VALUE="<?php echo $_POST['blah']?>";
    Page3:

    Read 'hidden' field "blah" (originally from Page1) as:
    PHP Code:
    $somevar $_SESSION['blah']; 
    Is this correct?

    My other problem with $salestax is still unresolved here.

  11. #11
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RocketMan
    So, you are saying that I can go to page2.php, perhaps add a 'hidden2' field, and then finally on page3.php I can read the initial page1.php 'hidden' tag using $_SESSION['fieldname']??? I thought this required $HTTP_POST_VARS, etc.
    If you store the information from page 1 in a session, then use $_SESSION[‘varname’]. If using a form, then use $_POST[‘varname’]. You need to keep those two things separated.

    Note: If PHP version is newer than 4.1.0 use $_POST[]. $HTTP_****_VARS is deprecated.

    Quote Originally Posted by RocketMan
    Exactly why do you have to "add a value to the session" ???
    If you don’t, the $_SESSION[‘varname’] would be empty, and of course of no use. The example was just to show you how the sessions are working.

    Quote Originally Posted by RocketMan
    OK...I think I am now understanding about the 'hidden' fields across pages.
    Good.

    Quote Originally Posted by RocketMan
    Page3:

    Read 'hidden' field "blah" (originally from Page1) as:
    PHP Code:
    $somevar $_SESSION['blah']; 
    Is this correct?
    Because you're sending the information through a form (with method="post") you need to use $_POST['blah'] instead of $_SESSION['blah'].

    Data sent using forms don’t get automatically put into a session.

    Quote Originally Posted by RocketMan
    My other problem with $salestax is still unresolved here.
    I’ll see if I can look into.

    HTH

    -Helge

  12. #12
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, again.

    About the salestax problem. You don't actually put any value into a $_SESSION['salestax']. Try:
    PHP Code:
    error_reporting(2039); // Temporary fix 
    ob_start(); 
    session_start();

    // $domain = $_SERVER['SERVER_NAME']; 
    $self = $_SERVER['PHP_SELF']; 

    if (isset($_POST['submit'])) {

        $taxrate = $_POST['taxrate'];
        $notaxrate = $_POST['notaxrate'];

        if (($taxrate == "info" ) && (!isset($notaxrate))) {
            echo "Please select a tax area.";
        } elseif (($taxrate != "info" ) && (isset($notaxrate))) {
            echo 'Please choose from only one set of options'; 
        } elseif (($taxrate != "info" ) && (!isset($notaxrate))) {
            $_SESSION['salestax'] = $taxrate;
            header('Location: test2.php'); // go to nextfile IF no error
        } elseif (($taxrate == "info" ) && (isset($notaxrate))) {
            $_SESSION['salestax'] = $notaxrate;
            header('Location: test2.php'); // go to nextfile IF no error
        } 
    } // END isset($submit) 

    ?> 
    <FORM METHOD="POST" ACTION="<?=$self?>"> 

    MY STATE (USA) CUSTOMERS ONLY:<br>
    Please Choose SHIP-TO DESTINATION Area<br>
    And Then Click The PROCEED Button Below<br>
    <SELECT NAME="taxrate" size="1">
        <OPTION VALUE="info" SELECTED>--Select SHIP-TO Destination--</OPTION>
        <OPTION VALUE=".0825">My Own County</OPTION>
        <OPTION VALUE=".0725">All OTHER State Counties</OPTION>
        <input type="hidden" name="taxcalc" value="taxrate">
    </SELECT>
    <br><br>

    NON-STATE CUSTOMERS:<br> 
    Please Check This Box And Then PROCEED<br>
    <input name="notaxrate" type="checkbox" value=".00">NON STATE CUSTOMER<br>
    <br>
    <INPUT name="submit" type="submit" value="PROCEED">
    &nbsp;&nbsp;&nbsp; 
    <INPUT name="reset" type="reset" value="Reset Form"> 
    </FORM> 
    <?php 
    ob_end_flush
    (); 
    ?>
    As you can see, I've moved the $_POST['xxx'] inside the if-clause aswell. If you don't, you'll get an undefined index error, which is not that serious, but it's nice trying not to have any errors I care about it, some don't.

    Now your script is hopefully working.

    -Helge

  13. #13
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, thank you Helge. I tried your modifications to my nextfile.php page of:
    PHP Code:
    <?php
    // nextfile.php
    session_start();
    error_reporting(2039); // Temporary fix
    $salestax $_SESSION['salestax'];
    echo 
    "Sales Tax: "."$salestax";
    ?>
    ...but $salestax is still not showing. All I get is:
    PHP Code:
    Sales Tax
    I am still doing something wrong???

    BTW, I do NOT like 'errors' either. Unfortunately, the only 'fix' for the problem on the other page someone gave me was: error_reporting(2039);

  14. #14
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm...it appears I may now have to go from:

    1. page1 (w/initial 'hidden' field info) TO:

    2. page2 (w/taxrate, etc. field info) TO:

    3. page3 (w/shipping, etc. field info) TO:

    4. page4 (final script page before Authorize.Net)

    But if we can get the current page working, then, in theory, I should be able to add (or) subtract pages as needed and everything will still work?

  15. #15
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh my gosh...hours and hours more experimenting and still NOT able to get $salestax to display (or use for further manipulation) on Page 2.

    There must be *some* way to do this.

    ARRRRGGGHHHHHHHH!!!

  16. #16
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    I'm starting to get a little confused about your problem.
    I think you have to make a desicion: Do you want to use session or do you want to use hidden fields to get the hidden data across the pages.

    Anyway, I've once more looked into the 'salestax' problem and the code below is working on my computer. I haven't made any major changes to what I posted above.

    page1.php
    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    ob_start(); 
    session_start();

    // $domain = $_SERVER['SERVER_NAME']; 
    $self $_SERVER['PHP_SELF']; 

    echo 
    "<p><a href=\"$self\">Reload</a></p>";

    if (isset(
    $_POST['submit'])) {

        
    $notaxrate = empty($_POST['notaxrate']) ? '' $_POST['notaxrate'];
        
    // See  php.net/manual/en/language.expressions.php
        // to get a explination about the line above
        
        
    $_SESSION['salestax'] = '';
        
        if ((
    $taxrate == "info" ) && (empty($notaxrate))) {
            echo 
    "Please select a tax area.";
        
        } elseif ((
    $taxrate != "info" ) && (!empty($notaxrate))) {
            echo 
    'Please choose from only one set of options'
        
        } elseif ((
    $taxrate != "info" ) && (empty($notaxrate))) {
            
    $_SESSION['salestax'] = $taxrate;
            
    header('Location: nextfile.php'); // go to nextfile IF no error
        
        
    } elseif (($taxrate == "info" ) && (!empty($notaxrate))) {
            
    $_SESSION['salestax'] = $notaxrate;
            
    header('Location: nextfile.php'); // go to nextfile IF no error
        

    // END isset($submit) 
    ?> 

    <form method="post" action="<?php echo $self?>"> 

    MY STATE (USA) CUSTOMERS ONLY:<br>
    Please Choose SHIP-TO DESTINATION Area<br>
    And Then Click The PROCEED Button Below<br>

    <select name="taxrate" size="1">
        <option value="info" selected>--Select SHIP-TO Destination--</option>
        <option value=".0825">My Own County</option>
        <option value=".0725">All OTHER State Counties</option>
        <input type="hidden" name="taxcalc" value="taxrate">
    </select>

    <br><br>

    NON-STATE CUSTOMERS:<br> 

    Please Check This Box And Then PROCEED<br>

    <input name="notaxrate" type="checkbox" value=".00" />NON STATE CUSTOMER<br><br>

    <button type="submit" name="submit">PROCEED</button>
    &nbsp;&nbsp;&nbsp; 
    <button type="reset" name="reset">RESET</button>
    </form> 

    <?php 
    ob_end_flush
    (); 
    ?>
    And nextfile.php
    PHP Code:
    <?php
    // nextfile.php
    error_reporting(E_ALL);
    session_start();

    echo 
    "<p><a href=\"page1.php\">Back to form (page 1)</a></p>";

    $salestax $_SESSION['salestax'];
    echo 
    "Sales Tax: $salestax";
    ?>
    If something is still unclear don't hesitate to ask. But I maybe suggest that you post the relevant code for every page of the "system", because I don't see how I could exlpain these things in a different way.

    -Helge

  17. #17
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello again, Helge:

    You are so very kind to help me with this nagging problem. Thanks.

    Well, I loaded the files but found several undefined variable errors showing, and also the Form 'error' message behaviors (user mistake type) are not functioning.

    I am running on a WIN2K Apache/MySQL/PHP 4.2.2 system for testing before going to the AIX Unix.

    This is a complete mystery to me!!!

    To answer your questions, part of my processing on some pages must be via Sessions and part via 'hidden' fields, which apparently makes things more complicated.

    Explanation:

    Page 1 - Initial Form with unique value=abc^blah^something^etc. 'hidden' field. This works fine in testing with an eventual list/explode manipulation on a subsequent page. No problem as it stands.

    However, what I have now determined is necessary is as follows:

    Page 1 - The Form w/'hidden' field mentioned above. BUT, if one of the ^fields^ will have a yes or no 'sales tax' code in it. If this is 'yes', then the action must go to Page 2 (Sales Tax). If 'no', then on to Page 3 (shipping page). If 'no' to both 'salestax' AND 'shipping', then processing goes directly to Page 4.

    Page 2 - SalesTax page. This is the form we have been dealing with which includes user-mistake type 'error checking', and depending upon the chosen option, a particular 'salestax' rate is passed to Page 3 (currently 'nextfile.php' for testing). But if the 'shipping' filed option from Page 1 is 'yes', then the next action is to go to Page 3 (shipping). Otherwise, if 'no', then directly to Page 4.

    Page 3 - Shipping page. This will be very similar to the Page 2 'Sales Tax' page...several user options and then processed to Page 4. There must be some user-mistake error checking like with Page 3.

    Page 4 - Final Processing BEFORE all variables/etc. are passed to the Authorize.Net "SIM" order page...and some final data manipulations. FYI, I can now go from Page 1 to Page 4 and on to Authorize.Net and testing works fine (only several fields).

    So, to summarize...

    I need to pass 'hidden' field data from Page 1 to Page 4...sometimes this will be directly, sometimes it will be via Page 2 and/or Page 3 on the way.

    But the $variables from Page 2 and Page 3 also need to be passed to Page 4 (and some $variables from Page 4 onto the AN page).

    The objective is to get it all working with just several fields (or even just ONE right now), and then I can expand each page from there. But it must all work 'error free' on my WIN2K system first since this is where any/all modifications will be made from start through the future.

    Does this now make more sense to you? Is this impossible? I hope not.

    Regards,

  18. #18
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    First: Sorry this is beeing a long post, with a lot of code.
    Quote Originally Posted by RocketMan
    You are so very kind to help me with this nagging problem. Thanks.
    No problem
    Quote Originally Posted by RocketMan
    Well, I loaded the files but found several undefined variable errors showing, and also the Form 'error' message behaviors (user mistake type) are not functioning.
    Strange. The files in my previous post works fine for me.

    Ok. I've tried to make system out of the four points in your last message. It's very simplified, but the main purpose is to show the basics. You can for sure add hidden fields, as long as you add the hidden field into session after the form/field is posted. As you see all forms are posted to $_SERVER['PHP_SELF']. That means I use redirect to get to next page when the form is ok on that particular page.

    Hopfully this is not going to confuse you but rather clear things a bit up

    Starting with page 1
    PHP Code:
    <?php
    ob_start
    (); 
    // page 1
    error_reporting(E_ALL);
    session_start();

    $self $_SERVER['PHP_SELF']; 
    echo 
    '<h2>Page 1</h2>';
    echo 
    "<p><a href=\"$self\">Reload</a></p>"

    if(isset(
    $_POST['submit'])) {
        if(isset(
    $_POST['salestax']) && isset($_POST['shipping'])) {
            
    // Adding data from form into session
            
    $_SESSION['salestax'] = $_POST['salestax'];
            
    $_SESSION['shipping'] = $_POST['shipping'];
            
    $_SESSION['hiddenvar'] = $_POST['hiddenvar'];
            
    $_SESSION['taxrate'] = false// Just to be sure it's empty if salestax is set to no
            
    $_SESSION['shiprate'] = false// Just to be sure it's empty if shipping is set to no
            
            
    if($_POST['salestax'] == 'yes') {
                
    // Go to page 2
                
    header('Location: page2.php');
            } else {
                if(
    $_POST['shipping'] == 'yes') {
                    
    // Go to page 3
                    
    header('Location: page3.php');
                } else {
                     
    // Go to page 4
                     
    header('Location: page4.php');
               }
            }
        } else {
            echo 
    '<p>You need to select both salestax and shipping!</p>';
        }
    }
    ?>


    <form method="post" action="<?php echo $self?>"> 
    <input type="hidden" name="hiddenvar" value="abc^blah^something" />

    <p>
    Salestax:<br />
    <input type="radio" name="salestax" value="yes">Yes<br />
    <input type="radio" name="salestax" value="no">No<br />
    </p>

    <p>
    Shipping:<br />
    <input type="radio" name="shipping" value="yes">Yes<br />
    <input type="radio" name="shipping" value="no">No<br />
    </p>

    <p>
    <button type="submit" name="submit">Send</button> 
    <button type="reset" name="reset">Reset</button> 
    </p>
    </form>
    <?php
    ob_end_flush
    ();
    ?>
    page 2
    PHP Code:
    <?php
    ob_start
    (); 
    // page 2
    error_reporting(E_ALL);
    session_start();

    $self $_SERVER['PHP_SELF']; 
    echo 
    '<h2>Page 2</h2>';
    echo 
    "<p><a href=\"$self\">Reload</a> :: "
    echo 
    '<a href="page1.php">Page 1</a></p>';

    echo 
    '<p>Variables send from page before:<br />';
    echo 
    'Salestax: ' $_SESSION['salestax'] . '<br />';
    echo 
    'Shipping: ' $_SESSION['shipping'] . '<br />';
    echo 
    'Hiddenvar: ' $_SESSION['hiddenvar'] . '<br /></p>';


    if(isset(
    $_POST['submit'])) {
        if(!isset(
    $_POST['taxrate'])) {
            echo 
    'Please select a tax area!';
        } else {
            
    // Adding data from form into session
            
    $_SESSION['taxrate'] = $_POST['taxrate'];
            
            if(
    $_SESSION['shipping'] == 'yes') {
                
    // Go to page 3
                
    header('Location: page3.php');
            } else {
                
    // Go to page 4
                
    header('Location: page4.php');
            }
        }
    }
    ?>


    <form method="post" action="<?php echo $self?>"> 
    <p>
    MY STATE (USA) CUSTOMERS ONLY:<br /> 
    Please choose SHIP-TO destination area<br />
    <input type="radio" name="taxrate" value=".0825">My own country<br />
    <input type="radio" name="taxrate" value=".0725">All other state Counties<br />
    </p>

    <p>
    NON-STATE CUSTOMERS:<br /> 
    <input type="radio" name="taxrate"  value=".00" />Please check this box
    </p>

    <p>
    <button type="submit" name="submit">Send</button> 
    <button type="reset" name="reset">Reset</button> 
    </p>
    </form> 
    <?php
    ob_end_flush
    ();
    ?>
    page 3
    PHP Code:
    <?php
    ob_start
    (); 
    // page 3
    error_reporting(E_ALL);
    session_start();

    $self $_SERVER['PHP_SELF']; 
    echo 
    '<h2>Page 3</h2>';
    echo 
    "<p><a href=\"$self\">Reload</a> :: "
    echo 
    '<a href="page1.php">Page 1</a> - ';
    echo 
    '<a href="page2.php">Page 2</a></p>';


    echo 
    '<p>Variables send from page before:<br />';
    echo 
    'Salestax: ' $_SESSION['salestax'] . '<br />';
    echo 
    'Shipping: ' $_SESSION['shipping'] . '<br />';
    echo 
    'Hiddenvar: ' $_SESSION['hiddenvar'] . '<br />';
    echo 
    'Taxrate: ' $_SESSION['taxrate'] . '<br /></p>';

    if(isset(
    $_POST['submit'])) {
        if(!isset(
    $_POST['shiprate'])) {
            echo 
    'Please select a destiantion!';
        } else {
            
    // Adding data from form into session
            
    $_SESSION['shiprate'] = $_POST['shiprate'];
        
            
    // Go to page 4
            
    header('Location: page4.php');
        }
    }
    ?>


    <form method="post" action="<?php echo $self?>"> 
    <p>
    <?php
    if($_SESSION['taxrate'] > 0) {
        echo 
    'Please select a state:<br />';
        echo 
    '<input type="radio" name="shiprate" value="20">Californaia<br />';
        echo 
    '<input type="radio" name="shiprate" value="20">Texas<br />';
        echo 
    '<input type="radio" name="shiprate" value="10">Illinois<br />';
        echo 
    '<input type="radio" name="shiprate" value="20">Florida<br />';
    } else {
        echo 
    'Please select a region:<br />';
        echo 
    '<input type="radio" name="shiprate" value="50">Europe<br />';
        echo 
    '<input type="radio" name="shiprate" value="70">Asia<br />';
        echo 
    '<input type="radio" name="shiprate" value="70">Oceania<br />';
        echo 
    '<input type="radio" name="shiprate" value="90">Africa<br />';
        echo 
    '<input type="radio" name="shiprate" value="50">South America<br />';
    }
    ?>
    </p>

    <p>
    <button type="submit" name="submit">Send</button> 
    <button type="reset" name="reset">Reset</button> 
    </p>
    </form> 
    <?php
    ob_end_flush
    ();
    ?>
    page 4
    PHP Code:
    <?php
    ob_start
    (); 
    // page 4
    error_reporting(E_ALL);
    session_start();

    $self $_SERVER['PHP_SELF']; 
    echo 
    '<h2>Page 4</h2>';
    echo 
    "<p><a href=\"$self\">Reload</a> :: "
    echo 
    '<a href="page1.php">Page 1</a> - ';
    echo 
    '<a href="page2.php">Page 2</a> - ';
    echo 
    '<a href="page3.php">Page 3</a></p>';

    echo 
    '<p>Variables send from page before:<br />';
    echo 
    'Salestax: ' $_SESSION['salestax'] . '<br />';
    echo 
    'Shipping: ' $_SESSION['shipping'] . '<br />';
    echo 
    'Hiddenvar: ' $_SESSION['hiddenvar'] . '<br />';
    echo 
    'Taxrate: ' $_SESSION['taxrate'] . '<br />';
    echo 
    'Shiprate: ' $_SESSION['shiprate'] . '<br /></p>';
    ob_end_flush();
    ?>
    Try this four files out and see if this is what your looking form. If you need further explination to the code or need help extending it, you know what to do.

    -Helge
    Last edited by Helge; Sep 16, 2003 at 02:55. Reason: Typo

  19. #19
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    RocketMan,

    I am not sure you really understand what sessions are yet. The session is a place where you can store variables that persist for the entire duration of a user browsing your site. Each session is unique for each user. If, in page 1, you set a variable called $_SESSION['salestax'] = .05; then it does not matter how many pages the user fetches or posts, when you hit page 100, with no interveaning hidden fields, you can still fetch $_SESSION['salestax'] and expect to see the value you placed there in page 1.

    Hidden fields cannot be trusted. They are easy for a user to fake. How would you like the user to substitute <input type="hidden" name="salestax" value="-100000"> ?

  20. #20
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank sweatje for emphasize that point.

    So to prevent from such things happeing you should check everything that is coming from the user. That means both information stored in session and sent from a form.

    To give an example. On page 2 you can replace the code checking the data from the form with this.
    PHP Code:
    if(isset($_POST['submit'])) {
        if(!isset(
    $_POST['taxrate'])) {
            echo 
    'Please select a tax area!';
        } else {
            
    $taxrates = array(.0825.0725.00);
            if(
    in_array($_POST['taxrate'], $taxrates)) {
                
    // Adding data from form into session
                
    $_SESSION['taxrate'] = $_POST['taxrate'];

                if(
    $_SESSION['shipping'] == 'yes') {
                    
    // Go to page 3
                    
    header('Location: page3.php');
                } else {
                    
    // Go to page 4
                    
    header('Location: page4.php');
                }
            } else {
                echo 
    'ERROR: Are you trying to change some values? Just stop. This system is bullet proof. *smile*';
            }
        }

    You should always think of issues like this, but you might want to sort the basics out first.

    -Helge

  21. #21
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hegle & sweatje:

    Thank you both for your assistance and alerts for concern.

    I will digest and test the code you kindly supplied, but also have some GREAT NEWS from which I also learned something new.

    (magiclab-Percy) on phpbuilder.com kindly supplied this as solution for getting $salestax to the next page, but used $_POST on the 1st page form instead of $_SESSION (which for some reason I could NOT get to work here):
    PHP Code:
    header("Location: nexturl.php?salestax=$salestax"); 
    So on the what is really nextfile.php, I put in:
    PHP Code:
    echo "Sales Tax: ".$HTTP_GET_VARS['salestax']; 
    ... and FINALLY saw $salestax displayed!!!!!!!!!!!

    So now I will test yours and if still a problem on my system with part, I do have an alternative (although if I want to pass TWO $variables instead of just $salestax, I'm not sure how to do that yet).

    You are very kind to have help me so much and I THANK YOU!!!

  22. #22
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Helge:

    I've done some preliminary testing with the pages, but here are a few of the results:
    PHP Code:
    PAGE 1:
    If 
    I select Yes Yeson Page 2 I get:
    ---------
    Variables send from page before:
    NoticeUndefined indexsalestax in page2.php on line 13
    Salestax

    NoticeUndefined indexshipping in page2.php on line 14
    Shipping

    NoticeUndefined indexhiddenvar in page2.php on line 15
    Hiddenvar

    ----------
    PAGE 2:
    If 
    I then select 'My own country'on Page 3 I get:
    ----------
    Variables send from page before:
    NoticeUndefined indexsalestax in page4.php on line 15
    Salestax

    NoticeUndefined indexshipping in page4.php on line 16
    Shipping

    NoticeUndefined indexhiddenvar in page4.php on line 17
    Hiddenvar

    NoticeUndefined indextaxrate in page4.php on line 18
    Taxrate

    NoticeUndefined indexshiprate in page4.php on line 19
    Shiprate
    :
    ----------
    PAGE 3:
    If 
    I select No Yes (on Page 1), I go directly to Page 3 and get:
    ----------
    Variables send from page before:
    NoticeUndefined indexsalestax in page3.php on line 15
    Salestax

    NoticeUndefined indexshipping in page3.php on line 16
    Shipping

    NoticeUndefined indexhiddenvar in page3.php on line 17
    Hiddenvar

    NoticeUndefined indextaxrate in page3.php on line 18
    Taxrate
    :
    NoticeUndefined indextaxrate in page3.php on line 37
    Please select a region
    :
    ----------
    PAGE 4:
    If 
    I select No No (on Page 1), I go directly to Page 4 and get:
    ----------
    NoticeUndefined indexsalestax in page4.php on line 15
    Salestax

    NoticeUndefined indexshipping in page4.php on line 16
    Shipping

    NoticeUndefined indexhiddenvar in page4.php on line 17
    Hiddenvar

    NoticeUndefined indextaxrate in page4.php on line 18
    Taxrate

    NoticeUndefined indexshiprate in page4.php on line 19
    Shiprate
    :
    ----------
    Etc., Etc
    I am not sure how to correct these error situations, and why they are showing on my machine but not yours!

  23. #23
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    RE:
    PHP Code:
    } else { 
    echo 
    'ERROR: Are you trying to change some values? Just stop. This system is bullet proof. *smile*'

    Cute

  24. #24
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RocketMan
    I am not sure how to correct these error situations, and why they are showing on my machine but not yours!
    I don't now either. Strange.

    The coded I posted works very well for me. It seems that you're having problem getting information stored a session. Just to do a small test. Try this two page.
    Test1.php
    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    session_start();

    $test 'Testing if the session is working.';

    echo 
    'Sentence that is put into the session: <br />';
    echo 
    $test;
    $_SESSION['test'] = $test;

    echo 
    '<p><a href="test2.php">Go to next page to see if the session is working</a></p>';
    ?>
    Test2.php
    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    session_start();

    echo 
    'If the session is working, the sentence on page one should be diplayed below.<br /><br />';
    echo 
    $_SESSION['test'];

    echo 
    '<p><a href="test1.php">Back</a></p>';
    ?>
    Those two files are working perfectly on my machine.

    To check your session settings you need to look at the phpinfo(). Create a file with the following:
    PHP Code:
    <?php
    phpinfo
    ();
    ?>
    Go to the SESSION part and see if 'Session Support' is 'enabled'.

    I'm starting running out of ideas now

    -Helge

  25. #25
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Helge:

    I checked...both my WIN2K and ISP's AIX Unix are configured for Sessions = Enabled.

    Tnx for the test code which I will try later. Must get back on the phone with regular business.

    Will advise.

    Regards,


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
  •