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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post

    select list problem

    Hi all,
    there is some stupid problem which i am not able to make out.i have a function to load months in a drop down. it loads properly but with a wrong seleted value.i am also passing the value which has to be selected by default but its not happening. i am not able to make it out
    below is my code:

    <?
    function DisplayMonths($field) {
    print("<option value='N/A'>N/A</option>");
    for ($i=1;$i<=12;$i++) {

    if ($i==$field) {
    $selected=' selected ';
    }

    switch ($i) {
    case 1:
    $mon="January";
    break;
    case 2:
    $mon="February";
    break;
    case 3:
    $mon="March";
    break;
    case 4:
    $mon="April";
    break;
    case 5:
    $mon="May";
    break;
    case 6:
    $mon="June";
    break;
    case 7:
    $mon="July";
    break;
    case 8:
    $mon="August";
    break;
    case 9:
    $mon="September";
    break;
    case 10:
    $mon="October";
    break;
    case 11:
    $mon="November";
    break;
    case 12:
    $mon="December";
    break;

    } // end of switch


    print("<option value=$i $selected>$mon</option>");

    }

    }


    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
    </HEAD>

    <BODY>
    <?$from=1;?>
    <select name="from" class="sel1"><?=DisplayMonths($from);?></select>
    </BODY>
    </HTML>

    there is some type caste problem??? please help me.December month gets selected instead of january

    Thanks in advance

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    PHP Code:
    $selected='';  //add this
    if ($i==$field) { 
    $selected=' selected ';

    you're setting $selected to equal 'selected' on the first pass, for january, but then it's still equal to 'selected' the next time round the loop. So all the options are ending up being 'selected', the browser must be picking the last one.

  • #3
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post

    Million Thanks!!!!!

    hi,
    thanks for ur reply.it was making me mad mad mad.its finally solved.



  •  

    Posting Permissions

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