SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Addict
    Join Date
    May 2007
    Location
    West Coast
    Posts
    337
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Stumped On Posting Selected Passed Variable to another page

    When you have a 2 page php form with just 1 or 3 select menus how would you get it to display as a "pre-selected" menu on the second page?

    Code:
    PAGE 1
    <form name="Test" action="submit" method="post">
    <input type="text" name="TEXT3" value="Value3" />
      <select name="OPTION1">
        <option value="OptionValue1">Option Description 1</option>
        <option value="OptionValue2">Option Description 2</option>
        <option value="OptionValue3">Option Description 3</option>
      </select>
    <select name="OPTION2">
        <option value="OptionValue4">Option Plan 4</option>
        <option value="OptionValue5">Option  Plan 5</option>
        <option value="OptionValue6">Option Plan 6</option>
      </select>
    
    <input type="text" name="course" value="" />
    
    <input name="submit" id="Submit">
    </form>

    PAGE 2:
    The only answer I have seen gives the input field echo answer below:

    <input type="text" name="course" value="<?php echo $course; ?>" /> which does work

    but the echo within a select field does not work (at least not for me)

    <option value="" <?php echo $_POST['Option1'] == "Chosen1" ? 'selected="selected"' : ''?>>Pick Me</option>
    <option value="" <?php echo $_POST['Option2] == "Unwanted" ? 'selected="selected"' : ''?>>No Me</option>

    I am looking for a pre-populated select field and then I will have another 6 questions for a final submit

    As of now, I am going to use the "<input type="text" name="Option1" value="<?php echo $option1; ?>" /> instead of an option value field.

    There is not a database, just a simple formmail on 2 pages with posted variables.

    anyone know what works properly?

  2. #2
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    281
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What happens if you use this as your second page, just to try out?
    Code:
    <?php
    // debug bits, see what the var contents actually are
    echo $_POST['TEXT3'];
    echo '\n';
    echo $_POST['OPTION1'];
    echo '\n';
    echo $_POST['OPTION2'];
    ?>
    
    <select name="OPTION1">
        <option value="OptionValue1" <?php echo $_POST['OPTION1']=="OptionValue1" ? 'selected="selected"' : ''; ?> >Option Description 1</option>
        <option value="OptionValue2" <?php echo $_POST['OPTION1']=="OptionValue2" ? 'selected="selected"' : ''; ?> >Option Description 2</option>
        <option value="OptionValue3" <?php echo $_POST['OPTION1']=="OptionValue3" ? 'selected="selected"' : ''; ?> >Option Description 3</option>
      </select>
    This works perfectly well for me. The key thing that makes this work where your code does not is $_POST['OPTION1'] - you've used $_POST['Option1'] in your sample above and that's not the same thing.
    http://www.firenza.net - my homage to a car from the 1970s

  3. #3
    SitePoint Addict
    Join Date
    May 2007
    Location
    West Coast
    Posts
    337
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by droopsnoot View Post
    What happens if you use this as your second page, just to try out?
    Code:
    <?php
    // debug bits, see what the var contents actually are
    echo $_POST['TEXT3'];
    echo '\n';
    echo $_POST['OPTION1'];
    echo '\n';
    echo $_POST['OPTION2'];
    ?>
    
    <select name="OPTION1">
        <option value="OptionValue1" <?php echo $_POST['OPTION1']=="OptionValue1" ? 'selected="selected"' : ''; ?> >Option Description 1</option>
        <option value="OptionValue2" <?php echo $_POST['OPTION1']=="OptionValue2" ? 'selected="selected"' : ''; ?> >Option Description 2</option>
        <option value="OptionValue3" <?php echo $_POST['OPTION1']=="OptionValue3" ? 'selected="selected"' : ''; ?> >Option Description 3</option>
      </select>
    This works perfectly well for me. The key thing that makes this work where your code does not is $_POST['OPTION1'] - you've used $_POST['Option1'] in your sample above and that's not the same thing.
    I've used the correct format, non capitalized, same as original select name and the second page only picks the 1st value so on page2 of form, the drop downmenu has Option Description 1, Option Description 1a, Option Description 1b, Option Description 1c, it will always pre-populate with Option Description 1 as the selection vs what the user selected on page 1.
    Seems to be an issue with the selected="selected" part.

    Would it matter if page2 is in a deeper directory? structured as follows; contact form is on right side of home page and inner pages.
    example.com > example.com/apply/page2.php
    example.com/miscpage > example.com/apply/page2.php

    also all pages are php includes like wp with header.php, navbar,php, content.php,footer.php, I am thinking I should make page2.php a straight html page with all code since it is echoing the user input. or am I mistaken?

  4. #4
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    281
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Position in the directory structure shouldn't make any difference - if that was a problem you'd get a 404.

    What happens if you insert the echo statements at the top of your second page - what values do you get for the option values? You need to check what the variables contain when they arrive at the second page, then we can see why they're not working properly in the <select>. Echo them to the screen and exit() straight away and go from there.
    http://www.firenza.net - my homage to a car from the 1970s

  5. #5
    SitePoint Addict
    Join Date
    May 2007
    Location
    West Coast
    Posts
    337
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, that went over my head. echo them to the screen?

    I used the echo code you game in the body area before the part 2 of the form.

    What displays on the screen is the 1st selected value, not what the user selected on page1

    aside from that, should this be in the header.php file?

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

  6. #6
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    281
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, just do:

    Code:
    echo $_POST['OPTION1'] .'\n';
    echo $_POST['whatver-name'].'\n';
    exit();
    at the start of your second page and see what it displays.

    That should give whatever values your user selected in the first form displayed on the screen, with nothing else. When I tried it with your code (after I added 'type="submit"' to get a submit button) I get the value from the 'option value=' code as you might expect, differing for each value I select.

    Can you show your actual code (or the relevant parts of it) rather than just something similar, or is it too big or some other issue? It's quite hard to debug without seeing exactly your code because the summary you posted is fine. If you're worried about whether the various includes are confusing matters, comment them out until you see what's happening.
    http://www.firenza.net - my homage to a car from the 1970s

  7. #7
    SitePoint Addict
    Join Date
    May 2007
    Location
    West Coast
    Posts
    337
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did that, input only

    <?php echo $_POST['Style'] .'\n';
    echo $_POST['State'].'\n';
    echo $_POST['Paint'].'\n';
    exit();
    ?>

    in the header area and after I select the options I want on page1, click, submit, my second page has only this at the top " \n\n\n"

    Code:
    
    <form method="post" action="reg-online/send" id="FormSide">
    <input name="viewstate" value="Do5yXr77Bym90VsOmHD37M" type="hidden" />
                                                
    	<div>
                                                <fieldset> 
    
    <!-- Loan Purpose/Amount --> <legend><font style="font-size: 10pt" color="#336699"><b>Just 2 Quick Steps</b></font></legend>
    												<div class="RegColumnHm">
    													<p><label class="hm">Preference</label></p>
    													<p><select name="Paint" id="Paint" tabindex="1" class="CustomFieldHome1" style="z-index: 0;">
    	<option selected="selected" value="">Select Below</option>
            <option value="Acrylic">Acrylic</option>
    	<option value="Enamel">Enamel</option>
    	<option value="Epoxy">Epoxy</option>
    
    </select></p>
    												</div>
    <div class="RegColumnHm">
    													<p><label class="hm">State
    															&nbsp;</label></p>
    													<p><select name="State" id="State" tabindex="2" class="CustomFieldHome1" style="z-index: 0;">
    	<option selected="selected" value="0">Select Below</option>
    	<option value="AL">Alabama</option>
    	<option value="AK">Alaska</option>
    	<option value="AZ">Arizona</option>
        </select>
        
        </select></p>
    												</div>
    <div class="RegColumnHm">
    													<p><label class="hm">Style Type</label></p>
    													<p><select name="Style" id="Style" tabindex="3" class="CustomFieldHome1" style="z-index: 0;">
    	<option value="" selected="selected">Select Below</option>
            <option value="Stripes">Stripes</option>
    	<option value="Purple">Purple</option>
    	<option value="Blue">Blue</option>
    	<option value="Orange">Orange</option>
    	<option value="Silver">Silver</option>
    
    </select></p>
    <p id="btn">
    <input name="submit" id="Submit" tabindex="4" src="http://www.approveflow.com/images/send_now_button.jpg" alt="" style="z-index: 0;" type="image" border="0"></p>
      <!-- Go to full Registration Page --> </div>
                        </fieldset>
                    </form>
    
    
    ////////////////  page 2 ////////////
    
    <?php
    $purpose = $_POST['Paint'];
    $state = $_POST['State'];
    $property = $_POST['Style'];
    
    echo $type;
    
    ?>
    <form name="reg" method="post" action="submit/paintdetails.php" id="Form12">
    
    <ul>
    <li>
    <fieldset>
    <div class="RegColumnHmi">
    <label class="med">Style</label>&nbsp;
    <input type="text" name="style" value="<?php echo $Style; ?>" />
    </div>
    </fieldset>
    </li>
    <li>
    <fieldset>
    <div class="RegColumnHmi">
    <label class="med">State											&nbsp;</label>
    <input type="text" name="state" value="<?php echo $State; ?>" />
    </div>
    </fieldset>
    </li>
    <li>
    <fieldset>
    <div class="RegColumnHmi">
    <label class="med">Paint Type</label>
    <input type="text" name="paint" value="<?php echo $Paint; ?>" />
    </div>
    </fieldset>
    </li>
    				<!-- Step 2 -->
    <li>
    <fieldset>
    <div class="RegColumnHmi">
    <label class="med">Square Feet
    															&nbsp;</label>
    <input name="Sqfeet" maxlength="15" size="12" id="Sqfeet" tabindex="13" class="CustomFitField"; type="text"> <span>1500 minimum</span>
    </div>
    </fieldset>
    </li>
    <!-- First Name/Last Name -->
    <li>
    <fieldset>
    <div class="RegColumnHmi">
    <label class="med"> Name
    															&nbsp;</label>
    <input name="Name" maxlength="30" size="20" id="Name" tabindex="1" class="CustomFitField"; type="text">
    </div>
    </fieldset>
    </li>
    <li>
    <fieldset>
    <div class="RegColumnHmi">
    <label class="med">Email Address</label>
     &nbsp;<input name="Email" maxlength="30" id="email" tabindex="7" class="CustomFitField"; type="text">
    </div>
    </fieldset>
    </li>
    												<!--
    <div class="RegColumn02">
    <p><label class="med">Zip Code<br />
    															&nbsp;</label></p>
    <p><input name="Zip" maxlength="5" id="ZIP" tabindex="6" class="CustomFitField"; type="text"></p>
    </p></div>
    <p>–><!-- Home Phone/Cell Phone -->
    <li>
    <fieldset>
    <div class="RegColumnHmi">
    <label class="med">Home Phone</label>
    &nbsp;<input name="tbHomePhone" maxlength="14" size="12" id="tbHomePhone" tabindex="8" class="CustomFitField"; type="text">
    <br><p class="iddy">ex. 555-555-1234 </p>
    </div>
    </fieldset>
    </li>
    
    
    <li>
    <fieldset>
    <p id="btn"><input type="hidden" name="config" value="0" />
    <input type="hidden" name="time" value="<?php echo time(); ?>" />
    <input type="text" name="nospam" value="" style="display:none;"/><input name="submit" id="btnSubmit" tabindex="8" src="http://www.approveflow.com/images/send_now_button.jpg" alt=""; type="image" border="0"><span id="lblError"><font color="Red"></font></span></p>
    </fieldset>
    </li>
    <li>
    </li>
    
    </ul>
    </form>
    </div>
     <!-- Registration Fields End ////////////////-->

  8. #8
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    281
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OK, so the variables aren't getting from the first page to the second page, they have no values there, so that's why the second page won't pre-select the option values, nothing to do with the 'selected' part.

    What happens if you replace the submit button with a proper 'input type="submit"' instead of using an image? I'm reading things that suggest using an image can cause some changes depending on which browser you use. I was testing with a standard submit button. Change it to that in the first page and see if that helps. If it does, you can use CSS to style the button to have an image.
    http://www.firenza.net - my homage to a car from the 1970s

  9. #9
    SitePoint Addict
    Join Date
    May 2007
    Location
    West Coast
    Posts
    337
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. Unfortunately, that did not change anything.

  10. #10
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    281
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well, I don't know. I've taken your code from the first page, changed the form post destination, and posted it into a file on my local wamp server. I've removed the extra '</select>' that appears after the state selection, but it worked before that as well.

    Code:
    <html>
    <body>
    <form method="post" action="sptest2.php" id="FormSide"> 
    <input name="viewstate" value="Do5yXr77Bym90VsOmHD37M" type="hidden" />
    <div> <fieldset>
    <!-- Loan Purpose/Amount -->
    <legend><font style="font-size: 10pt" color="#336699"><b>Just 2 Quick Steps</b></font></legend> 
    <div class="RegColumnHm"> 
    <p><label class="hm">Preference</label></p> 
    <p>
    <select name="Paint" id="Paint" tabindex="1" class="CustomFieldHome1" style="z-index: 0;">
      <option selected="selected" value="">Select Below</option> 
      <option value="Acrylic">Acrylic</option>
      <option value="Enamel">Enamel</option> 
      <option value="Epoxy">Epoxy</option>
      </select>
    </p> </div>
    <div class="RegColumnHm">
    <p><label class="hm">State &nbsp;</label></p> 
    <p>
    <select name="State" id="State" tabindex="2" class="CustomFieldHome1" style="z-index: 0;"> 
      <option selected="selected" value="0">Select Below</option>
      <option value="AL">Alabama</option> 
      <option value="AK">Alaska</option> 
      <option value="AZ">Arizona</option> 
    </select> </p>
    </div>
    <div class="RegColumnHm"> <p><label class="hm">Style Type</label></p>
    <p>
    <select name="Style" id="Style" tabindex="3" class="CustomFieldHome1" style="z-index: 0;"> 
      <option value="" selected="selected">Select Below</option> 
      <option value="Stripes">Stripes</option>
      <option value="Purple">Purple</option> 
      <option value="Blue">Blue</option>
      <option value="Orange">Orange</option> 
      <option value="Silver">Silver</option> 
    </select></p> 
    <p id="btn"> 
    </p> <!-- Go to full Registration Page --> </div> </fieldset>
    <input type="image" name="submit" id="Submit" src="test.jpg" />
    </form>
    </body>
    </html>
    Obviously I don't have the CSS or anything, but when I post it to sptest2.php:

    Code:
    <?php
    // debug bits, see what the var contents actually are
    echo $_POST['State'].'\n';
    echo $_POST['Style'].'\n';
    echo $_POST['Paint'].'\n';
    exit();
    ?>
    I get

    Code:
    AK\nBlue\nEnamel\n
    But I notice in your sample code above you're changing the variable names again -

    Code:
    <?php
    $purpose = $_POST['Paint'];
    $state = $_POST['State'];
    $property = $_POST['Style'];
    echo $type;
    ?>
    where did $type come from? And later in the same code you echo $Style, $State and $Paint, not $property, $state and $purpose that you used in the start of the page. That's what I meant about seeing the 'actual' code, it's hard to see what might be wrong if this is just a paraphrased version.
    http://www.firenza.net - my homage to a car from the 1970s

  11. #11
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    281
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    And yes, I was hoping that '\n' would throw a newline, not just display \n. Wrong language.
    http://www.firenza.net - my homage to a car from the 1970s

  12. #12
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    281
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Does it matter that your destination in the first page ("reg-online/send") is not specifically a .php extension? Will the server treat it as php and process it without the name ending in .php? If you change it and rename to send.php, does that help?
    http://www.firenza.net - my homage to a car from the 1970s

  13. #13
    SitePoint Addict
    Join Date
    May 2007
    Location
    West Coast
    Posts
    337
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not sure if that will help and this is my first attempt at using 5 separate files to make one page (header, footer, body, navigation, doctype)
    these are just php includes files without any html extension on the end when displayed. (like wordpress)

    PHP Code:


    <?php 
      
    // Edit the following three lines of code. '$a' adjusts the relative paths of the page. '$current" tells the Nav Bar what the current page is. '$thisPageContent' should match the filename in the includes > 6.content folder.
        
    $a '../';
        
    $current 6;
        
    $thisPageContent 'applications.php';
        
        
    // Do not change the following line of code. You can go into the '0.seo.php' file to edit SEO content.
        
    include ($a 'includes/0.seo.php');
        
        
    // Edit the following three lines of code. You will want to match the variables after the equals sign to the variables in '0.seo.php'.
        
    $thisPageTitle $applicationsTitle;
        
    $thisPageKeywords $applicationsKeywords;
        
    $thisPageDescription $applicationsDescription;
    ?>

    <?php
        
        
    include ($a 'includes/0.sitelinks.php');
        include (
    $a 'includes/1.doctype.php');
    ?>
    <head>
    <?php include ($a 'includes/2.head.php'); ?>

      <title><?=$thisPageTitle?></title>
            
      <meta name="keywords" content="<?=$thisPageKeywords?>">    
      <meta name="description" content="<?=$thisPageDescription?>">
      
    </head>

    <body>

    <?php 
      
    include ($a 'includes/3.top.php'); 
      include (
    $a 'includes/4.navbar.php'); 
      include (
    $a 'includes/6.content/' $thisPageContent ); 
      include (
    $a 'includes/7.footer.php'); 
      include (
    $a 'includes/8.bottom.php'); 
    ?>

    </body>
    </html>
    OK, how would I make this page php? ..... by changing the bottom area and top?
    since it is not saveable in DW as a .php, I am not sure how to go about this.
    Last edited by SpacePhoenix; Oct 1, 2013 at 14:34. Reason: placed php tags around php code

  14. #14
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    281
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What I actually meant was that the file name doesn't end in ".php", so I wondered how the server would know that it should process the file - perhaps your server processes all files to see if they have php code in them. If I rename my second file to 'sptest2' without the .php extension and modify my destination, my wamp server does not process the php and all I get is the code displayed on the screen.

    Do the includes work, is the additional content included? I guess it must be if the 'echo' statements were working as you said earlier. If the php code in the second file is executed rather than displayed, it can't be that.

    I don't know, I'm out of ideas. I am not familiar with Dreamweaver, it always concerns me when I hear that you can't control what filename you use for stuff because of the editor and I've steered clear of them because of that kind of thing.

    Anyone else have any ideas?
    http://www.firenza.net - my homage to a car from the 1970s

  15. #15
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    281
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Is there any kind of redirection going on from page 1 to page 2, or is page 2 actually a php file called 'send' in a folder called 'reg-online'?
    http://www.firenza.net - my homage to a car from the 1970s

  16. #16
    SitePoint Addict
    Join Date
    May 2007
    Location
    West Coast
    Posts
    337
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, the file does redirect and I guess it does process it as php since we had values before. I think I'll have to make the second page a full html page (not separate files being combined) and save as .php although the site we got the idea from does not do that. They are on WP and use gravity forms. So, I can't understand why this site needs to have a .php extension when that one doesn't to execute a 2 page form.

    I can process the page specifically as a php with this code
    Code:
    <Files first.htm>
        AddType application/x-httpd-php .htm
    </Files>
    but there is no extension that I can see as the site appears like wordpress sites without an extension.

  17. #17
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    281
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well, maybe it's the redirection that is causing the problem - does it preserve post variables after redirection.

    I can't see the includes being the issue, because when you put the debugging echo statements in they were at the top of the file before the includes, they were clearly processed as you got the \n display, but no variable values.

    I have no experience of Wordpress and that kind of site so I can't offer anything further. It will be interesting to hear if merging all the include files into one makes a difference, please come back and post the results.
    http://www.firenza.net - my homage to a car from the 1970s

  18. #18
    SitePoint Addict
    Join Date
    May 2007
    Location
    West Coast
    Posts
    337
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, that was my thought too as I did merge all includes files into one but no difference, just blank fields when they should be populated.
    the form I am trying to emulate is va*loan*center dot net a short form with 3 drop down select questions. which sends those same selections to page2.

    I've seen code dating back to 2006 in this concept but it doesn't work. Most likely I am doing something wrong.

    so, this is what I have
    page1 has the form, 3 select menus (actually all pages in the site have it) it redirects to:
    page2 (not a php extension file due to php includes) . Although, I did insert the

    Code:
    <?php
    session_start();
    // other php code here
    
    $_SESSION['One'] = $One;
    $_SESSION['Two'] = $Two;
    $_SESSION['Three'] = $Three;
    ?>
    at the top before doctype and nothing changed
    It is processing as mentioned above but not echoing or posting.


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
  •