473,327 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

How to get original combo value

ddtpmyra
333 100+
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.

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 { 
  6.  
  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.    } 
  18.  
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>
  3.  
Thanks,
DM
Jan 15 '09 #1
8 3443
Atli
5,058 Expert 4TB
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:
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";
  6.  
  7. // Print the top of the select box
  8. echo '<select name="animalSelect">';
  9.  
  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.   }
  19.  
  20.   // Add the option to the select box
  21.   echo "<option value="{$_option}" {$selected}>{$_option}</option>";
  22. }
  23.  
  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
ddtpmyra
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.

thanks,
DM

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.    }    
  20.  
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
Atli
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
ddtpmyra
333 100+
Hi Atli,

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

thanks,
DM
Jan 22 '09 #5
Atli
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
ddtpmyra
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
ddtpmyra
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
  1.  
  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 "
  9.  
  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.      } 
  21.  
Jan 26 '09 #8
Atli
5,058 Expert 4TB
Glad you got it working :)
Jan 26 '09 #9

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

Similar topics

3
by: vgrssrtrs | last post by:
<html> <head> <script language="JavaScript"> <!-- /* *** Multiple dynamic combo boxes *** by Mirko Elviro, 9 Mar 2005 *** ***Please do not remove this comment
5
by: Harry Haller | last post by:
<select name="cboPlaces" id="cboPlaces"> <option value="3">Countryside</option> <option value="4">Forest</option> <option value="5">Mountain</option> <option value="6">Desert</option> <option...
1
by: meganrobertson22 | last post by:
Hi Everyone- I am trying to use a simple macro to set the value of a combo box on a form, and I can't get it to work. I have a macro with 2 actions: OpenForm and SetValue. I can open my form,...
1
by: James | last post by:
I am used to VB6 but need to develop something in .Net. I need to create several bound combo-boxes which will use lookup tables to get their values. I created a form using the dataform wizard....
0
by: Cndla | last post by:
hi, I have two combo boxes in a form. Functionality is such that selecting a value from the first combo box changes the value in the second combo box. Now this works fine in a normal computer with...
8
by: salad | last post by:
I was wondering how you handle active/inactive elements in a combo box. Let's say you have a combo box to select an employee. Joe Blow has been selected for many record however Joe has left the...
8
by: AA Arens | last post by:
Hi I do have a products table and products-parts table in my Access 2003 database and log all services into a form. I do have at least the following two combo boxes on my form: - Choose...
0
by: Dawnyy | last post by:
I have a form which is bound to a dataset. I am filling the forms dataset on Form_Load event. On my form I have combo boxes which I am setting by running a stored procedure to return a datatable,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.