472,347 Members | 2,316 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,347 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 3336
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...
5
by: Harry Haller | last post by:
<select name="cboPlaces" id="cboPlaces"> <option value="3">Countryside</option> <option value="4">Forest</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...
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...
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...
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...
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...
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

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.