#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    34
    Rep Power
    2

    Retain Values after submit


    Hello,

    I am developing a User Registration form. I this form, I have two combo boxes, which are populated with numbers as follows:

    Code:
     
            <label  for="texp" >Total Experience</label>
            
            <label  for="texpyears" >Years</label>
    	    <select id="texpyears" name="texpyears">
    	    <?php for($i = 0; $i <= 50; ++$i) 
    	              printf('<option value="%d">%d</option>', $i, $i);
    	    ?>
    	    </select>
            <label  for="texpmonths" >Months</label>	    
    	   	<select id="texpmonths" name="texpmonths">
    	    <?php for($i = 0; $i <= 11; ++$i)
    	              printf('<option value="%d">%d</option>', $i, $i);
    	    ?>
    	    </select>
    After I press the Register button, during the validation error comes, the values selected by the user are gone and it shows the first value 0. How can I retain the selected values in the combo box.

    In another place in the same Registration Form, I have solved this issue by using the following code. But here the options are manually added. So it was easy. But in the code above, the options are added in a loop. Please show me how to achieve the same in this case. Thanks.

    Code:
            <select name="purpose" class="text">
              <option value="Enquiry" 
        		<?php if(isset($_POST['purpose']) && ($_POST['purpose'] == 'Enquiry')) 
        			echo 'selected="selected"'; 
        		?>
       >Enquiry</option>
              <option value="Exam Registration" 
        		<?php if(isset($_POST['purpose']) && ($_POST['purpose'] == 'Exam Registration')) 
        			echo 'selected="selected"'; 
        		?>  
      
      >Exam Registration</option>
            </select>
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,122
    Rep Power
    9398
    It really is the exact same thing but this time you do it in a loop: if the years/months value is set and matches the $i from your loop, mark the <option> as selected.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    just think about the general logic: You build a list of values. You have the old value. How do you preselect the old value in the list?

    You'd simply compare each value to the old one, and if they match, you'd preselect the value, right? As pseudo code:

    Code:
    each value in possible_values:
    	is_preselected := false
    	if value == POST['old_value']:
    		is_preselected := true
    	print_option(value, is_preselected)
    Translating this into PHP code shouldn't be hard.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    34
    Rep Power
    2
    I tried this now and it doesn't work.

    Code:
    	    <select id="texpyears" name="texpyears">
    	    <?php for($i = 0; $i <= 50; ++$i) 
    	              printf('<option value="%d">%d</option>', $i, $i);
    	              
    	          for($i = 0; $i <= 50; ++$i)
    		          if(isset($_POST['texpyears']) && ($_POST['texpyears'] == $i)) echo 'selected="selected"';
    	    ?>
    	    </select>
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Where does the selected attribute go?

    You currently have this:

    html Code:
    <option>
    <option>
    ...
    <option>
     
    'selected="selected"

    That doesn't look right. The attribute must be in one of the option tags, right?

    Check the pseudo code again. It's one loop with a check in it. You can't write down the complete list and then do the check, because it's already too late to insert the attribute.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    34
    Rep Power
    2
    I did this in sevaral ways as the following code, but none of them work. I have tried changing "selected" to $i, but doesn't work. I am new to php language, by the way.



    Code:
    	    <select id="texpyears" name="texpyears">
    	    <?php for($i = 0; $i <= 50; ++$i) 
                  {
    	              printf('<option value="%d">%d</option>', $i, $i);
                         if(isset($_POST['texpyears']) && ($_POST['texpyears'] == $i)) echo 'selected="selected"';
                  }
    	    ?>
    	    </select>


    Code:
    	    <select id="texpyears" name="texpyears" class="text">
    	    <?php for($i = 0; $i <= 50; ++$i) 
    	    {
    	              printf("<option value='%d'
    	             if(isset(\$_POST['texpyears']))
    				    {
    				    	if(\$_POST['texpyears'] == \$i) echo 'selected=\"selected\";
    				    }
    	              
    	              >%d</option>", $i, $i);
    		 }         
    	    ?>
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    This doesn't really have anything to do with PHP. As I've shown above, you can describe this without a single line of PHP code.

    The attribute has to be in the HTML element. So you either have to print everything in one go. Or you first have to print one part of the element, then the attribute and then the rest of the element.

    The second snippet kinda goes into the right direction, but you cannot embed code in a string (how is that supposed to work?). I guess you're missing the obvious:

    PHP Code:
    $selected_attribute '';
    if (isset(
    $_POST['texpyears']) && ($_POST['texpyears'] == $i))
        
    $selected_attribute 'selected="selected"';

    printf('<option value="%d" %s>%d</option>'$i$selected_attribute$i); 
    Of course you can also do this in other ways like printing the separate parts or concatenating them or whatever.

    You should also read up on how to escape input for HTML so that your website won't be vulnerable to JavaScript injections.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    34
    Rep Power
    2
    Thanks a lot. Here is the corrected code:

    Code:
    	    <?php 
    
        		for($i = 0; $i <= 50; ++$i) 
        		{
        		  $selected_attribute = '';
        			
    	          if(isset($_POST['texpyears']))
    	          { if($_POST['texpyears'] == $i) 
    	          	$selected_attribute = 'selected="selected"'; 
    	          }
    
    	          printf('<option value="%d" %s>%d</option>', $i, $selected_attribute, $i);  
        		}
    	    ?>
  16. #9
  17. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Not sure why you're now using this nested if statement. Just use the logical "and" like before.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    34
    Rep Power
    2
    Just to increase readability. Not a big deal. Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo