Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help in selected drop-down list doesn't show

    Hi. Please help with my code. I have a drop down list selected using an array to select the month and year for expiration date. The problem is, when I select the month and year and some fields has error, the page refresh but the selection I made for the month is gone and it goes back to the default which is the first selected Index as "--". Below is my array as it is written:
    PHP Code:
    global $expiry_month;
    $expiry_month = array(
        
    ""    =>    "--",
        
    "01"    =>    "01",
        
    "02"    =>    "02",
        
    "03"    =>    "03",
        
    "04"    =>    "04",
        
    "05"    =>    "05",
        
    "06"    =>    "06",
        
    "07"    =>    "07",
        
    "08"    =>    "08",
        
    "09"    =>    "09",
        
    "10"    =>    "10",
        
    "11"    =>    "11",
        
    "12"    =>    "12",
        
        );

    global 
    $expiry_year;
    $expiry_year = array(
        
    ""    =>    "--",
        
    "09"    =>    "09",
        
    "10"    =>    "10",
        
    "11"    =>    "11",
        
    "12"    =>    "12",
        
    "13"    =>    "13",
        
    "14"    =>    "14",
        
    "15"    =>    "15",
        
    "16"    =>    "16",
        
    "17"    =>    "17",
        
    "18"    =>    "18",
        
    "19"    =>    "19",
        
    "20"    =>    "20",
        
        ); 
    Here is my form code for the expiration date:
    PHP Code:
    <select id="expiry_month" name="expiry_month" style="width:50px;">
    <?php foreach ($expiry_month as $monthselect => $month) : ?>
          <option value="<?php echo $monthselect?><?php echo helper_selected($monthselect == $form['expiry_month']); ?>><?php safeEcho($month); ?></option>
    <?php endforeach; ?>
        </select>
        
                    <select name="expiry_year" id="expiry_year"  style="width:50px;" onChange="expiredate() ;" selected='' >
                        <?php foreach($expiry_year as $year =>$prompt) : ?>
                          <option value = "<?php echo $year?>
                              <?php echo helper_selected($year == $form['expiry_year']); ?>
                             >
                              <?php safeEcho($prompt); ?>
                          </option>  
                          <?php endforeach;?>
                    </select>
    I want if possible to show what I selected originally when the page refresh for any error on the other page like for example if I selected 12 for month and 10 for year I wanted to show the 12 10 when the page refresh but it doesn't. What did i do wrong with this? I spent almost how many days to figure out but I couldn't. Please, I really need to make this work as soon as possible. Please help!!!! Thanks in advance.

  • #2
    Regular Coder
    Join Date
    Dec 2007
    Posts
    107
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Have you thought of using sessions to store the selected values. So that next time when the page refreshes the values from session are checked for the drop down box.

    Also while refreshing are you doing post back to the server? in that case you may read the drop down values selected prior to refreshing the page and check these values against the values in drop down.
    Programming & Webmaster Forum - AdSense Revenue Sharing forum

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,864
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Write an if condition to check the value in th posted data against the value from your foreach loop and add an attribute like selected="selected" for the matched one..
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    New Coder
    Join Date
    Oct 2009
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by technica View Post
    Have you thought of using sessions to store the selected values. So that next time when the page refreshes the values from session are checked for the drop down box.

    Also while refreshing are you doing post back to the server? in that case you may read the drop down values selected prior to refreshing the page and check these values against the values in drop down.
    Hi technica, thanks for your help. Yes, I did try SESSION and POST but both still doesn't work. and the worst is, instead of the first array that will show when it is a new page, it shows the last number which is the 12. I don't understand why it is giving me this problem knowing that my other form is working just fine with the same data. Is there any suggestions? Thanks.

  • #5
    New Coder
    Join Date
    Oct 2009
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi abduraooft, thanks for your suggestion. I actually use the if condition and use
    Code:
    selected="selected"
    but it shows the last number only and it didn't change at all. Although it is capturing the number to my server which is giving me the right selection, I wanted to show it to the user what they selected but it doesn't work. Please help! I am desperate to make this work. Thanks.

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,864
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    That attribute has to be applied on the required <option> tag and not on the <select> tag
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #7
    New Coder
    Join Date
    Oct 2009
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks a lot for your help abduraooft but it really doesn't work. I tried to include the
    Code:
    selected="selected"
    in the
    Code:
    [option][/option]
    but still didn't work. Please help I need to make this work tomorrow. Thanks in advance.

  • #8
    New Coder
    Join Date
    Oct 2009
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is there any other way to do it instead of using PHP? I guess it is better if I use regular HMTL but it will be long, can somebody please advice? I need to make this work by tomorrow and I am really behind as I am spending lots of time just to make this problem work. Please help!!!!! Thanks.

  • #9
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    PHP Code:
    <?php echo helper_selected($year == $form['expiry_year']); ?>
    Is that even a valid argument for helper_selected?

    Please try:
    PHP Code:
    <?php echo helper_selected($form['expiry_year']); ?>
    If that doesn't work, make sure you're returning a $form array with the selected month and year when an "error" has been found, in whichever php file you're using to validate and process the form.

  • #10
    New Coder
    Join Date
    Oct 2009
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    HI. Sorry to reply this late. I actually never make it to work but will try again next time. If I come out with a solution I will post it here. Thanks for all your help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •