Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 10-26-2009, 11:24 AM   PM User | #1
jmarian1
New Coder

 
Join Date: Oct 2009
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
jmarian1 is an unknown quantity at this point
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.
jmarian1 is offline   Reply With Quote
Old 10-26-2009, 11:38 AM   PM User | #2
technica
New Coder

 
Join Date: Dec 2007
Posts: 88
Thanks: 0
Thanked 3 Times in 3 Posts
technica is an unknown quantity at this point
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 - Win $5 every week on technicaltalk.net for answering solutions
Forum Post Exchange Resource Forum
technica is offline   Reply With Quote
Old 10-26-2009, 12:56 PM   PM User | #3
abduraooft
Master Coder

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: God's own country
Posts: 8,976
Thanks: 142
Thanked 1,192 Times in 1,184 Posts
abduraooft is a jewel in the roughabduraooft is a jewel in the roughabduraooft is a jewel in the roughabduraooft is a jewel in the rough
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..
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 10-26-2009, 09:37 PM   PM User | #4
jmarian1
New Coder

 
Join Date: Oct 2009
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
jmarian1 is an unknown quantity at this point
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.
jmarian1 is offline   Reply With Quote
Old 10-26-2009, 09:41 PM   PM User | #5
jmarian1
New Coder

 
Join Date: Oct 2009
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
jmarian1 is an unknown quantity at this point
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.
jmarian1 is offline   Reply With Quote
Old 10-27-2009, 08:50 AM   PM User | #6
abduraooft
Master Coder

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: God's own country
Posts: 8,976
Thanks: 142
Thanked 1,192 Times in 1,184 Posts
abduraooft is a jewel in the roughabduraooft is a jewel in the roughabduraooft is a jewel in the roughabduraooft is a jewel in the rough
That attribute has to be applied on the required <option> tag and not on the <select> tag
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 11-04-2009, 02:17 AM   PM User | #7
jmarian1
New Coder

 
Join Date: Oct 2009
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
jmarian1 is an unknown quantity at this point
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.
jmarian1 is offline   Reply With Quote
Old 11-04-2009, 03:01 AM   PM User | #8
jmarian1
New Coder

 
Join Date: Oct 2009
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
jmarian1 is an unknown quantity at this point
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.
jmarian1 is offline   Reply With Quote
Old 11-04-2009, 04:42 AM   PM User | #9
shadowmaniac
Regular Coder

 
Join Date: Apr 2005
Posts: 100
Thanks: 0
Thanked 31 Times in 31 Posts
shadowmaniac is an unknown quantity at this point
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.
shadowmaniac is offline   Reply With Quote
Old 11-11-2009, 11:06 PM   PM User | #10
jmarian1
New Coder

 
Join Date: Oct 2009
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
jmarian1 is an unknown quantity at this point
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.
jmarian1 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:50 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.