473,373 Members | 1,119 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,373 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 3445
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.