469,906 Members | 2,206 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,906 developers. It's quick & easy.

How to get original combo value

333 100+

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.

Expand|Select|Wrap|Line Numbers
  1. echo '<tr><td><b>How was event lead generated:</b></td>'; 
  2. $res=mysql_query("select lead  from tblgenerated"); 
  3. if(mysql_num_rows($res)==0){ 
  4. echo "there is no data in table.."; 
  5. } else { 
  7.  echo '  <td width="100%"><select name="lead" id="lead">'; 
  8.    for($i=0;$i<mysql_num_rows($res);$i++) { 
  9.    $row=mysql_fetch_assoc($res); 
  10.    echo"<option value=$row[lead]"; 
  11.    if($Var==$row[lead]) 
  12.    echo 'selected'; 
  13.    echo ">$row[lead]</option>"; 
  14.    } 
  15.    echo 
  16. "</select><br></tr></td>"; 
  17.    } 
And I know how to do it on text box but not on combo (see below) Please help.
Expand|Select|Wrap|Line Numbers
  1. <td colspan="2"><input type="text" name="location" ' . 'id="entry_location" 
  2. size="85" value="' . htmlspecialchars ( $location )     .  '" />  </td>
Jan 15 '09 #1
8 3147
5,058 Expert 4TB

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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // The values
  3. // These would ideally come from your database. I will leave that part up to you.
  4. $options = array("Cat", "Dog", "Rabbit");
  5. $selectedOption = "Rabbit";
  7. // Print the top of the select box
  8. echo '<select name="animalSelect">';
  10. // Print each option
  11. foreach($options as $_option) 
  12. {
  13.   // Determine if this is the selected option.
  14.   // Add the "selected" attribute if it is.
  15.   $selected = "";
  16.   if($_option == $selectedOption) {
  17.     $selected = 'selected="selected"';
  18.   }
  20.   // Add the option to the select box
  21.   echo "<option value="{$_option}" {$selected}>{$_option}</option>";
  22. }
  24. // Close the select box
  25. echo '</select>';
  26. ?>
Which should produce: (Without the white-spaces, that is)
Expand|Select|Wrap|Line Numbers
  1. <select name="animalSelect">
  2.   <option value="Cat" >Cat</option>
  3.   <option value="Dog" >Dog</option>
  4.   <option value="Rabbit" selected="selected">Rabbit</option>
  5. </select>
So your browser should initially have "Rabbit" selected.
Jan 16 '09 #2
333 100+
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.


Expand|Select|Wrap|Line Numbers
  1. echo "Generated By:"; 
  2. //my query for combo selection
  3. $res=mysql_query("select GeneratedBy  from tblGeneratedBy"); 
  4. if(mysql_num_rows($res)==0){ 
  5. echo "there is no data in table.."; 
  6. } else { 
  7. //my combo box
  8. echo "
  9. <td width='80%'><select name=\"generated_by\" id=\"generated_by\">"; 
  10.    for($i=0;$i<mysql_num_rows($res);$i++) { 
  11.    $row=mysql_fetch_assoc($res); 
  12.    echo"<option value=\"$row[GeneratedBy]\""; 
  13.    if($Var==$row[GeneratedBy]) 
  14.    echo "selected"; 
  15.    echo ">$row[GeneratedBy]</option>"; 
  16.    } 
  17.    echo 
  18. "</select><br></tr></td>"; 
  19.    }    
I tried to to directly puting the value on but didn't work
Expand|Select|Wrap|Line Numbers
  1. <td width='100%'><select name=\"contact\" id=\"contact\" value=\"$contact\" >";
Jan 16 '09 #3
5,058 Expert 4TB
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.
Jan 17 '09 #4
333 100+
Hi Atli,

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

Jan 22 '09 #5
5,058 Expert 4TB
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.
Jan 23 '09 #6
333 100+
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?
Jan 26 '09 #7
333 100+
Hi Atli,

It's now working I just have to use my variable from my query wich is status. Thanks for your help.
Expand|Select|Wrap|Line Numbers
  2. echo "
  3.   <tr><td width='100%'><b>Status</b></td>"; 
  4.   $res=mysql_query("select EventStatus  from tblEventStatus"); 
  5.   if(mysql_num_rows($res)==0){ 
  6.   echo "there is no data in table.."; 
  7.   } else { 
  8.   echo "
  10.   <td width='80%'><select name=\"status\" id=\"status\">"; 
  11.      for($i=0;$i<mysql_num_rows($res);$i++) { 
  12.      $row=mysql_fetch_assoc($res); 
  13.      echo"<option value=$row[EventStatus]"; 
  14.      if($status==$row[EventStatus]) 
  15.      echo " selected"; 
  16.      echo ">$row[EventStatus]</option>"; 
  17.      } 
  18.      echo 
  19.   "</select><br></tr></td>"; 
  20.      } 
Jan 26 '09 #8
5,058 Expert 4TB
Glad you got it working :)
Jan 26 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by vgrssrtrs | last post: by
1 post views Thread by meganrobertson22 | last post: by
1 post views Thread by James | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.