www.webdeveloper.com
Results 1 to 7 of 7

Thread: Retaining Drop Down List Selection after a form submit

  1. #1
    Join Date
    Mar 2012
    Location
    Battle Ground, Washington
    Posts
    30

    Retaining Drop Down List Selection after a form submit

    I have this drop down list. I am trying to capture the selected value of the drop down and have it remain selected after a submit. I have tried to work with this but I never got the syntax correct. Any ideas?

    Code:
    <!DROP DOWN #2>
    			<select name="item2" style="width: 250px">
    				<option value="60">DAILY MAX TEMPS &ge; 60</option>
    				<option value="70">DAILY MAX TEMPS &ge; 70</option>
    				<option value="80">DAILY MAX TEMPS &ge; 80</option>
    				<option value="90">DAILY MAX TEMPS &ge; 90</option>
    				<option value="32">DAILY MAX TEMPS &le; 32</option>
    				<option value="monthlyhighs">MONTHLY AVG MAX TEMPS</option>
    				<option value="monthlylows">MONTHLY AVG MIN TEMPS</option>
    				<option value="raindays">DAY OF RAINFALL PER MONTH</option>
    				<option value="spring_occ">FIRST SPRING OCCURENCES</option>
    				<option value="rainfall">MONTHLY/YEARLY RAINFALL</option>
    				
    				<!The below code is supposed to retain the selected drop down but does not>
                      <option value="<?php $_POST['item2']
    				  echo 'selected="selected"'?>><?php echo ['item2']?></option>

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    First, you need to insert an option at the very beginning of all the options in the select, give it a value of blank (ie, ""), and text of "Select" or something like that.

    Next, param the value of that select to be blank (again, "") if it doesn't already exist. (That way, if it does exist - as it should after submit - it won't be over-written.)

    Thirdly, each option within the select needs to have an if/then condition added to it that checks the value of the submitted against the value of the option - if it matches, echo or print " selected='true'" inside the option tag for that value.

  3. #3
    Join Date
    Mar 2012
    Location
    Battle Ground, Washington
    Posts
    30
    Using the code from this thread as a model http://www.daniweb.com/web-developme...hp-form-submit
    I came up with the following code that works.. woo hoo.. thank you very much.
    I have one more question however. The value retained in the drop down after selection is the value submitted to the $_POST array. I was hoping to be able to populate the drop down with the named value instead.
    For instance:
    I get the value in bold below returned. What I want is the magenta text in the box. Any ideas? I am so close here.
    <option value="60">DAILY MAX TEMPS &ge; 60</option>


    Code:
    	  <!DROP DOWN #2>
    <select name="item2" id="item2" style="width: 250px">
    			
    <?php if($_POST['submit2'] == true){ ?>
     <option value="<?php echo $_POST['item2']; ?>" selected="selected"><?php echo $_POST['item2']; ?></option>
     <?php }else{ ?>
     <option value=""> -- select -- </option>
      <?php } ?>
     <option value="<?php echo $_POST['item2']; ?>" selected="selected"><?php echo $_POST['item2']; ?></option>
     <option value="60">DAILY MAX TEMPS &ge; 60</option>
     <option value="70">DAILY MAX TEMPS &ge; 70</option>
     <option value="80">DAILY MAX TEMPS &ge; 80</option>
     <option value="90">DAILY MAX TEMPS &ge; 90</option>
     <option value="90">DAILY MAX TEMPS &ge; 90</option>
     <option value="32">DAILY MAX TEMPS &le; 32</option>
     <option value="monthlyhighs">MONTHLY AVG MAX TEMPS</option>
     <option value="monthlylows">MONTHLY AVG MIN TEMPS</option>
     <option value="raindays">DAY OF RAINFALL PER MONTH</option>
     <option value="spring_occ">FIRST SPRING OCCURENCES</option>
     <option value="rainfall">MONTHLY/YEARLY RAINFALL</option>

  4. #4
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    The value is what the form submits, not the text.

  5. #5
    Join Date
    Mar 2012
    Location
    Battle Ground, Washington
    Posts
    30
    Trying to get a third drop down to work on my form. Cannot get it to post the image. I think there is something wrong with my if/else if statement

    Here is the code for process.php:
    Code:
    <?php
    error_reporting(E_ALL ^ E_NOTICE) ;
    //If button 1 is clicked
    if($_POST['item']) {
      $item = $_POST['item']; }
    else if ($_POST['item']){
      $item = 2011; 
       $item = $_POST['item']; }
      //if button 2 is clicked
      if($_POST['item2']) {
      $item2= $_POST['item2'];}
    //if button 3 is clicked
      if($_POST['item3']) {
      $item3= $_POST['item3']; 
      }?>
    
      <?php
    if($_POST['Submit1']){ 
        $url = "<img src='/WXRECORDS/images/yearly/{$_POST['item']}.png' />"; 	
    }elseif($_POST['Submit2']){ 
        $url = "<img src='/WXRECORDS/images/frequency/{$_POST['item2']}.jpg' />"; 
    }else($_POST['Submit3']){ 	
         $url ="<img src='/WXRECORDS/images/monthly/{$_POST['item3']}.jpg' />";}
    //else{ 
       // $url = "<img src='/WXRECORDS/images/yearly/2011.jpg' />"; 
    //} 
    ?> 
    <?php include_once('portland.html');?>
    Here is the code for my HTML form: Portland.html
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
       <head>
          <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
          <title>Portland Climate Data</title>
          <link href="stylesheets/public.css" rel="stylesheet" type="text/css" >
       </head>
       <body style="height: 53px; color: #FFFFFF; background-color: #000000;">
          <form  action="process.php" method="post">
             <div id="banner" class="banner">Portland Oregon Climate Data </div>
             <div id="header" class="header" style="height: 35px; width:1000px" align="center">
          
    <!DROP DOWN #1>
    		<select name="item" style="width: 65px">
              <?php for($i=2011; $i>=1949; $i--){?>
    			<option value="<?php echo $i?>" <?php if($i==$_POST['item'])
    			echo 'selected="selected"'?>><?php echo $i?></option>
              <?php }?>
            </select>  
    		  
    <!BUTTON #1>
              <input name="Submit1" type="submit" value="submit" style="height: 28px" /> &nbsp 
    			
    <!DROP DOWN #2>
            <select name="item2" id="item2" style="width: 250px">
    		  <?php if($_POST['submit2'] == true){ ?>
    			<option value="<?php echo $_POST['item2']; ?>" selected="selected"><?php echo $_POST['item2']; ?></option>
    		  <?php }else{ ?>
    			<option value=""> -- select -- </option>
    		  <?php } ?>
    			<option value="<?php echo $_POST['item2']; ?>" selected="selected"><?php echo $_POST['item2']; ?></option>
    			<option value="60">DAILY MAX TEMPS &ge; 60</option>
    			<option value="70">DAILY MAX TEMPS &ge; 70</option>
    			<option value="80">DAILY MAX TEMPS &ge; 80</option>
    			<option value="90">DAILY MAX TEMPS &ge; 90</option>
    			<option value="90">DAILY MAX TEMPS &ge; 90</option>
    			<option value="32">DAILY MAX TEMPS &le; 32</option>
    			<option value="monthlyhighs">MONTHLY AVG MAX TEMPS</option>
    			<option value="monthlylows">MONTHLY AVG MIN TEMPS</option>
    			<option value="raindays">DAY OF RAINFALL PER MONTH</option>
    			<option value="spring_occ">FIRST SPRING OCCURENCES</option>
    			<option value="rainfall">MONTHLY/YEARLY RAINFALL</option>
    			
    <!BUTTON #2>
    		<input name="Submit2" type="submit" id="submit2" value="submit" style="height: 28px" /> &nbsp 
    		  
    <!DROP DOWN #3>
    		<select name="item3" id="item3" style="width: 150px">
    		  <?php if($_POST['submit3'] == true){ ?>
    			<option value="<?php echo $_POST['item3']; ?>" selected="selected"><?php echo $_POST['item3']; ?></option>
    		  <?php }else{ ?>
    			<option value=""> -- select -- </option>
    		  <?php } ?>
    			<option value="<?php echo $_POST['item3']; ?>" selected="selected"><?php echo $_POST['item3']; ?></option>
    			<option value="January-July">January-July</option>
    			<option value="July-December">July-December</option>
    
    <!BUTTON #3>
            <input name="Submit3" type="submit" id="submit3" value="submit" style="height: 28px" />	
    		</div><br /></form>     
    	  <div id="content" align="center" class="content"><?php echo $url ?></div></body></html>

  6. #6
    Join Date
    Mar 2012
    Location
    Battle Ground, Washington
    Posts
    30
    here is an online version of my php project:

    http://www.directpaintsaleswa.com/br...DS/process.php

    Take a look at it.. You can see how the box repopulates with the field value instead of the selected name.

  7. #7
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    A select box (unlike a radio or checkbox) exists, even if there is nothing selected. Instead of checking true/false, check value. If it has a length of 0, it wasn't populated with a value.

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