www.webdeveloper.com
Results 1 to 15 of 15

Thread: Pass select option values to 2nd page of form - Help

  1. #1
    Join Date
    Sep 2013
    Posts
    9

    Pass select option values to 2nd page of form - Help

    I want to know how to do this. I have been puzzled for a few days now and I am still new at PHP. I understand how to make it appear as input field but not select.

    OK, the site is being built as follows:

    each page has an index.php files with php includes files:
    doctype.php
    header.php
    navbar.php
    body.php
    footer.php

    I have on all pages the form with 3 select options.

    site.com > submit/registration.php
    and
    site.com/innerpage > submit/registration.php


    Like what this site
    Code:
    valoancenter.net
    has on front page
    3 select menu options, whatever the user selects on that page is saved and chosen on the select fields on the 2nd part (or page) of the form

    My problem is getting those "3" selected values to be chosen again on the 2nd page.

    This is not sessions, but rather echo


    an example in page 2 of the form of how echo was used.
    Code:
    <li>
    <fieldset>
    <div class="RegColumnHmi">
    <label class="med">State</label>
    <select name="State" id="State" tabindex="5";>
    <option value="AL" <?php echo $_POST['State'] == "AL" ? 'selected="selected"' : "'?>>Alabama</option>
    <option value="AK" <?php echo $_POST['State'] == "AK" ? 'selected="selected"' : ''?>>Alaska</option>
    just above the form on 2nd page
    Code:
     
    <?php
    $purpose = $_POST['Purpose'];
    $state = $_POST['State'];
    echo $type;
    ?>
    now there is nothing in the header with people I am newbie with PHP.
    Should something else be there?

    Since my site acts like WP with example.com/page2, the 2nd page is not .php. so

    <?php
    //let's start the session /////////
    ?>

    I didn't want or think this needed sessions. Any help is appreciated.

    Thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,331
    In the first option tag, you mixed a double quote with a single quote for the final empty string part of the ternary expression.

    To make it a bit more robust, I might change it to:
    PHP Code:
    <select name="State" id="State" tabindex="5";>
    <option value="AL" <?php
        
    echo (!empty($_POST['State']) and $_POST['State'] == "AL")
        ? 
    'selected="selected"' ''
    ?>>Alabama</option>
    <option value="AK" <?php
        
    echo (!empty($_POST['State']) and $_POST['State'] == "AK")
        ? 
    'selected="selected"' ''
    ?>>Alaska</option>
    </select>
    (The line breaks are there for clarity, not necessarily required.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Sep 2013
    Posts
    9
    Quote Originally Posted by NogDog View Post
    In the first option tag, you mixed a double quote with a single quote for the final empty string part of the ternary expression.

    To make it a bit more robust, I might change it to:
    PHP Code:
    <select name="State" id="State" tabindex="5";>
    <option value="AL" <?php
        
    echo (!empty($_POST['State']) and $_POST['State'] == "AL")
        ? 
    'selected="selected"' ''
    ?>>Alabama</option>
    <option value="AK" <?php
        
    echo (!empty($_POST['State']) and $_POST['State'] == "AK")
        ? 
    'selected="selected"' ''
    ?>>Alaska</option>
    </select>
    (The line breaks are there for clarity, not necessarily required.)
    This just gave me the 1st item in the select menu, not what was selected on previous page

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,331
    Double-check the spelling (case-sensitive) of the "state" field being passed from the first page's form.

    Verify the first form is passing it as post data, not as get data.

    Try some debugging to see if you're even receiving it:
    PHP Code:
    echo "<pre>DEBUG:\n".print_r($_POSTtrue)."</pre>"// or $_GET ? 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Sep 2013
    Posts
    9
    isn't there a simpler way?

    I have a form action="post" on the right side of page, small area (with just 3 select menu options to choose, then goes to 2nd page). I would like to prefill these same 3 fields on the second pages using values from fields on the first form.

    Code:
    <?php
    session_start();
    // other php code here
    
    $_SESSION['Purpose'] = $Purpose;
    $_SESSION['State'] = $State;
    ?>
    My site is all php includes files. I have tried sessions above the doctype field, I have tried echo as well.

    I get to 2nd page but they are not filled in? there is not a database. I just wanted it as simple as eating a piece of pie.

    What will work for sure?

  6. #6
    Join Date
    Sep 2013
    Posts
    9
    I cannot update site until it works. please if anyone knows a simple way to get it working. I am not a php guy so sending me bits of code may be over my knowledge.

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,331
    There is nothing wrong in theory with your approach. Without any debug info to tell us what is wrong, I do not know how to help you.

    Are you calling session_start() on all PHP pages involved in this process?

    You could possibly have an issue with sub-domain versus just domain in URLs. That can be avoided by changing the session.cookie_domain in your PHP config to be ".yourdoamin.com" (note the leading "."), or in each script before calling session_start():
    PHP Code:
    <?php
    session_set_cookie_params
    (0'/''.yourdomain.com'); // note leading "." for domain
    session_start();
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Sep 2013
    Posts
    9
    Since this mini form will be on all pages, I didn't want a php session on all pages. I thought you could do 1st page html, but 2nd page must be .php with echo post, no need for sessions. However, neither works.
    subdomain problem? Why? because of this structure

    example.com (mini form) > example/register/pre.php fields of miniform and 6 more input questions)

  9. #9
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,331
    Any page that touches $_SESSION must have session_start() first. If the very first page is just submitting the form and does not itself touch $_SESSION, then no, it does not need a session_start().

    (That being said, I find it easier to just make everything a PHP page, so I don't have to worry about changing it later when requirements change. The overhead to process a .php page with no actual PHP code in it is effectively negligible.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  10. #10
    Join Date
    Sep 2013
    Posts
    9
    So, if the structure of all pages is as follows:

    1. webpage name is carpet - this is a folder with an index.php file in it that is created from all include php files (header,nav, body, footer.php files)

    This means webpage carpet is a php page. the extension is not displayed and display as example.com/carpet in the address bar
    also, since the index file is not named index.html, it is php. I am a beginner but not that bad.

  11. #11
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,331
    Sounds like it should be fine. This, of course, assumes that PHP is actually running on your host? (If there's any question of that, just do a "view source" of one of your pages and make sure you don't see any PHP source code on them.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  12. #12
    Join Date
    Sep 2013
    Posts
    9
    already did view source many times days ago. Now, it seems a particular includes file is not being processed

  13. #13
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,331
    Well there's your problem. (I hope?)

    If it's necessary that that file be included, you might want to use require() instead of include(), as the former will generate a fatal error if it cannot find/read the specified file. Also, to help in debugging, it's often useful to turn on all error-reporting. You can disable it later for the live version once everything is good:
    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    ini_set('display_errors'true); // set to false for production version
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  14. #14
    Join Date
    Sep 2013
    Posts
    9
    inserting that code just gave me Undefined index for name that are in the select menu.

  15. #15
    Join Date
    Sep 2013
    Posts
    9
    I have followed many examples of this concept and it still does not work.

    The php code is being processed (from viewing source code) but not displayed

    is thus because the 2nd page of the form is in a different directory?

    site.com > site.com/reg/form.php ?

    or site.com/2ndpage > site.com/reg/form.php ?

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
  •  
HTML5 Development Center



Recent Articles