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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Resetting drop down menu values

    After the user clicks a button, I would like to reset the chosen value of a drop down menu. For example, if I have the following drop down menus:
    <?
    for (i =0; i<7; i++){
    ?>
    <span class="style1">Out</span>
    <td width=""><select id="stop<? echo $i ?>" name= "stop<? echo $i ?>">
    <option value="0" default></option>
    <option value="0">12:00 AM</option>
    <option value="0.5">12:30 AM</option>
    <option value="1">1:00 AM </option>

    <?
    }
    ?>

    How can I change the option value from 0 to 12:00 AM so that A12:00 AM gets saved to the database (and not the 0)? Is it possible in php so that I can do something like this:

    stop1 option value = stop1 option text

    I have 14 drop down menus with each having 24 options, so if I could something like that statement it would be very help.

    Please note, for a good reason (and I dont want to get into because its just a long story), I cannot recode the above option values to the following (if I could, I wouldnt be posting this):

    <option value="12:00 AM">12:00 AM</option>
    <option value="12:30 AM">12:30 AM</option>
    <option value="1:00 AM">1:00 AM </option>

    Any help is greatly appreciated! =)

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    3 options:
    javascript, but since this is clientsided you could then just as well change the values (just hypothising about your reasons to no use the desires values)

    server side scripting --> when you build the dropdown, use 0,1,2,3,4,... as values. Then you create an array like

    $drp_realvalues= array('12:00 AM', '12:30 AM', '1:00 AM');

    (note : if the dropdown is build dynamically, then you can create this inside the same loop as you create the dropdown)


    Now, when the form is posted, you go

    $realvalue = array('dummy');
    for (i =0; i<7; i++){
    $realvalue[] = $drp_realvalues[$_POST['stop' . $i]] ;
    }

    $realvalue[1] now contains the same value as the label of dropdown 1

    Use a db Store the different times inside a db-table. Then use the primary key value of the record as value inside the dropdown
    --> additional advantage, you can create the records randomly (or assign the pk values yourself in a random order)

    to get the value (if you still realy need it) then you go

    sql="select timelabel from timetable where timeID=" . $_POST['stop' . $i] ;
    Last edited by raf; 03-27-2004 at 09:59 AM.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    New to the CF scene
    Join Date
    Jul 2003
    Location
    Minnesota
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if I understand your problem correctly wouldn't it be possible to remove the value for the default select and have a pice of code on the submit page like this?
    Code:
    <?php
    if($stop1 == "0"){
        // Add 12.00 AM to the database
    }
    ?>

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have 14 drop down menus with each having 24 options,
    So you would need at least 24 if's and 24 different ''// Add 12.00 AM to the database'' + a loop for the 14 dropdowns, and that is then even just if they all have the same values. Otherwise, you'll need more then 24 if's then need to be evaluated all 14 times.
    I don't even see how you would tackle the dropdown-identification in your code.

    That's why i thought it would be better to set up one array. i strongly suspect all 14 dropdowns have the same values. But even if that isn't true, you can still build 1 array wth the union off all values.

    Processing the form for all 14 dropdown then takes 5 lines
    PHP Code:
    $realvalue[]=array('0'=>'dummy');
    foreach(
    $_POST as $var=>$val){
       if (
    substr($var,0,4)=='stop'){
          
    $drplabel=array(substr($var,4)=>$drp_realvalues[$val]); 
          
    $realvalue[] = $realvalue $drplabel;
      }

    after wich you have a nice array with the labels and the drpnumbers as keys. And you could even have 100 dropdowns, the code wouldn't be needing any change.
    If the value needs to ne processed or inserted, then you can do that inside the loop istead of building the array.
    It will be much faster because i only need one evaluation for each formfield to filter out the dropdowns and then it's just a lookup from the array.

    But i personally would use the db-approach becasue you then don't need any conversion. Just selec PK-value (used as option-value) and label when building the array, and then insert the PK-value when processing, also in a foreach-loop.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

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