How to get original combo value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ddtpmyra
    Contributor
    • Jun 2008
    • 333

    How to get original combo value

    Hi,

    I have PHP script that I use for new entry and edit entry, inside the form I have query combo (combo values querying from other table), my problem is how make the default value of my combo to actual value on the edit table instead on picking the first value on my query.

    For instance I have query combo CAT, DOG, RABBIT.
    1st time entry user choose RABBIT and then save, then user go back to edit page this time the combo will display the last value which is RABBIT instead of CAT as first choice. I hope Im explaining this very well let me know if you need more info.

    Code:
    echo '<tr><td><b>How was event lead generated:</b></td>'; 
    $res=mysql_query("select lead  from tblgenerated"); 
    if(mysql_num_rows($res)==0){ 
    echo "there is no data in table.."; 
    } else { 
     
     echo '  <td width="100%"><select name="lead" id="lead">'; 
       for($i=0;$i<mysql_num_rows($res);$i++) { 
       $row=mysql_fetch_assoc($res); 
       echo"<option value=$row[lead]"; 
       if($Var==$row[lead]) 
       echo 'selected'; 
       echo ">$row[lead]</option>"; 
       } 
       echo 
    "</select><br></tr></td>"; 
       }
    And I know how to do it on text box but not on combo (see below) Please help.
    Code:
    <td colspan="2"><input type="text" name="location" ' . 'id="entry_location" 
    size="85" value="' . htmlspecialchars ( $location )     .  '" />  </td>
    Thanks,
    DM
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    Ok. I don't know how your tables are set up or how you fetch your data from them, so let me just offer a generic example:
    [code=php]
    <?php
    // The values
    // These would ideally come from your database. I will leave that part up to you.
    $options = array("Cat", "Dog", "Rabbit");
    $selectedOption = "Rabbit";

    // Print the top of the select box
    echo '<select name="animalSel ect">';

    // Print each option
    foreach($option s as $_option)
    {
    // Determine if this is the selected option.
    // Add the "selected" attribute if it is.
    $selected = "";
    if($_option == $selectedOption ) {
    $selected = 'selected="sele cted"';
    }

    // Add the option to the select box
    echo "<option value="{$_optio n}" {$selected}>{$_ option}</option>";
    }

    // Close the select box
    echo '</select>';
    ?>[/code]
    Which should produce: (Without the white-spaces, that is)
    [code=html]
    <select name="animalSel ect">
    <option value="Cat" >Cat</option>
    <option value="Dog" >Dog</option>
    <option value="Rabbit" selected="selec ted">Rabbit</option>
    </select>[/code]
    So your browser should initially have "Rabbit" selected.

    Comment

    • ddtpmyra
      Contributor
      • Jun 2008
      • 333

      #3
      Hi Atli,

      Sorry, I'm confused im not very knowledgeable on PHP but heres my script, please tell me how or what part of the script should I make changes to make the last selection static.

      thanks,
      DM

      Code:
      echo "Generated By:"; 
      //my query for combo selection
      $res=mysql_query("select GeneratedBy  from tblGeneratedBy"); 
      if(mysql_num_rows($res)==0){ 
      echo "there is no data in table.."; 
      } else { 
      //my combo box
      echo "
      <td width='80%'><select name=\"generated_by\" id=\"generated_by\">"; 
         for($i=0;$i<mysql_num_rows($res);$i++) { 
         $row=mysql_fetch_assoc($res); 
         echo"<option value=\"$row[GeneratedBy]\""; 
         if($Var==$row[GeneratedBy]) 
         echo "selected"; 
         echo ">$row[GeneratedBy]</option>"; 
         } 
         echo 
      "</select><br></tr></td>"; 
         }
      I tried to to directly puting the value on but didn't work
      Code:
      <td width='100%'><select name=\"contact\" id=\"contact\" value=\"$contact\" >";

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Where does the $Var value come from? I don't see it anywhere in the code except where you use it in the loop.

        To have an <option> selected by default, that option has to be created with the "selected" attribute. Like in the example I posted earlier.
        Adding the "value" attribute to the <select> box doesn't really have any effect.

        What needs to happen in your code is, you need to find which of the options is meant to be selected by default. Then, when you loop through all the options, you need to compare the option that is meant to be selected to the option that you are currently printing. If they match, you add the "selected" attribute to that option.

        Your code already seems to do that, but the selected option, which in your code is $Var, needs to be set first. So before you print the options, you would have to query your database for the option you want to be initially selected, and put that value into the $Var variable.

        Comment

        • ddtpmyra
          Contributor
          • Jun 2008
          • 333

          #5
          Hi Atli,

          Need more help in here...can you show me how to do this out of my code?

          thanks,
          DM

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Your code essentially works, you just need to assign the value you want to be selected by default to the $Var variable.

            I don't really know you database well enough to be able to tell you how to do that.

            Comment

            • ddtpmyra
              Contributor
              • Jun 2008
              • 333

              #7
              Hi Atli,

              On my script above, I have separate table for the option select values and actual table I want to edit my entry. should i make another sql statement on my main table to display the current value?

              Comment

              • ddtpmyra
                Contributor
                • Jun 2008
                • 333

                #8
                Hi Atli,

                It's now working I just have to use my variable from my query wich is status. Thanks for your help.
                Code:
                 
                echo "
                  <tr><td width='100%'><b>Status</b></td>"; 
                  $res=mysql_query("select EventStatus  from tblEventStatus"); 
                  if(mysql_num_rows($res)==0){ 
                  echo "there is no data in table.."; 
                  } else { 
                  echo "
                 
                  <td width='80%'><select name=\"status\" id=\"status\">"; 
                     for($i=0;$i<mysql_num_rows($res);$i++) { 
                     $row=mysql_fetch_assoc($res); 
                     echo"<option value=$row[EventStatus]"; 
                     if($status==$row[EventStatus]) 
                     echo " selected"; 
                     echo ">$row[EventStatus]</option>"; 
                     } 
                     echo 
                  "</select><br></tr></td>"; 
                     }

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Glad you got it working :)

                  Comment

                  Working...