oceans Posted April 24, 2007 Share Posted April 24, 2007 Dear People, Can any one help me in “ListBox”. I know how to get info from the list box and process. Now the challenge is; when a data comes from the data base, the list box should select show the value. By default, first entry comes first right! I have NOT done this before in ASP as well. Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/ Share on other sites More sharing options...
ToonMariner Posted April 24, 2007 Share Posted April 24, 2007 compare the listitem value to the item you have processed - if they match put selected="selected" inside the <option> tag. Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-236812 Share on other sites More sharing options...
oceans Posted April 25, 2007 Author Share Posted April 25, 2007 Dear Friend, You have done the search portion, I am doing the same, please help me with the selection portion. Lets say an example: " <td><select name="Combo2" size="1" class="WorkPageField" id="Combo2"> <option value="Yes">Yes</option> <option value="No">No</option> </select></td> " The above is with the values "Yes" and "No". My data from DataBase is "No", how shell I instruct the ListBox to Show "No", if not it will show "Yes", by default due it being the first Entry. Thanks. Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237711 Share on other sites More sharing options...
sanfly Posted April 25, 2007 Share Posted April 25, 2007 <td><select name="Combo2" size="1" class="WorkPageField" id="Combo2"> <option value="Yes" <? if($theValue = "Yes"){ echo "selected"; } ?>>Yes</option> <option value="No" <? if($theValue = "No"){ echo "selected"; } ?>>No</option> </select></td> change $theValue to the name of the variable you are trying to match Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237716 Share on other sites More sharing options...
oceans Posted April 25, 2007 Author Share Posted April 25, 2007 Dear SanFly, Not working, maybe some twitching in your code? Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237726 Share on other sites More sharing options...
sanfly Posted April 25, 2007 Share Posted April 25, 2007 Yep, sorry, forgot to double up the = signs Should be: <td><select name="Combo2" size="1" class="WorkPageField" id="Combo2"> <option value="Yes" <? if($theValue == "Yes"){ echo "selected"; } ?>>Yes</option> <option value="No" <? if($theValue == "No"){ echo "selected"; } ?>>No</option> </select></td> Dont forget to ctrl+F5 to refresh the page after making the changes. You need the form to reset itself properly Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237730 Share on other sites More sharing options...
oceans Posted April 25, 2007 Author Share Posted April 25, 2007 SanFly, Still no Luck. The Entries in the box become ">Yes" and ">No". I have assigned "$theValue = "No";" at the top of body's PHP Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237735 Share on other sites More sharing options...
sanfly Posted April 25, 2007 Share Posted April 25, 2007 Can you post your ENTIRE code please Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237738 Share on other sites More sharing options...
oceans Posted April 25, 2007 Author Share Posted April 25, 2007 Thanks SanFly, " <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <?PHP //Transfer Data from Screen to Memory $NumberOfTxtBoxes=5; $NumberOfTxtBoxesToBeFilled=5; $theValue = "No"; if (isset($_POST['Submit'])) { for ($i=1; $i<=$NumberOfTxtBoxes; $i++) { $InputFromScreen[$i]=strtoupper($_POST["Txt".$i]); } } else { for ($i=1; $i<=$NumberOfTxtBoxes; $i++) { $InputFromScreen[$i]=""; } } ?> <form id="form1" name="form1" method="post" action="Untitled-1.php"> <table width="200" border="1"> <tr> <td> </td> <td><input name="Txt1" value="<?PHP echo $InputFromScreen[1]; ?>" type="text" id="Txt1" /></td> </tr> <tr> <td> </td> <td><input name="Txt2" value="<?PHP echo $InputFromScreen[2]; ?>" type="text" id="Txt2" /></td> </tr> <tr> <td> </td> <td><input name="Txt3" value="<?PHP echo $InputFromScreen[3]; ?>" type="text" id="Txt3" /></td> </tr> <tr> <td> </td> <td><input name="Txt4" value="<?PHP echo $InputFromScreen[4]; ?>" type="text" id="Txt4" /></td> </tr> <tr> <td> </td> <td><input name="Txt33" value="<?PHP echo $InputFromScreen[5]; ?>" type="text" id="Txt33" /></td> </tr> <tr> <td> </td> <td><select name="Combo2" size="1" class="WorkPageField" id="Combo2"> <option value="Yes" <? if($theValue == "Yes"){ echo "selected"; } ?>>Yes</option> <option value="No" <? if($theValue == "No"){ echo "selected"; } ?>>No</option> </select></td> </tr> <tr> <td> </td> <td><select name="select" size="1" class="WorkPageField" id="select"> <option value="Yes">Yes</option> <option value="No">No</option> </select></td> </tr> <tr> <td> </td> <td><?PHP ?></td> </tr> <tr> <td> </td> <td><input type="submit" name="Submit" value="Submit" /></td> </tr> </table> </form> </body> </html> " Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237740 Share on other sites More sharing options...
sanfly Posted April 25, 2007 Share Posted April 25, 2007 Works for me Did you press ctrl + F5 to refresh the page? Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237742 Share on other sites More sharing options...
oceans Posted April 25, 2007 Author Share Posted April 25, 2007 Friend, I am surprised, Got i got to add anything like <?php ob_start(); ?> before the page starts, now i do not have any. Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237747 Share on other sites More sharing options...
sanfly Posted April 25, 2007 Share Posted April 25, 2007 Sorry, I dont understand what you mean Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237749 Share on other sites More sharing options...
boo_lolly Posted April 25, 2007 Share Posted April 25, 2007 you should also look at your html source code to see what's going on there. this is what it should look like: <?php echo " <select name=\"Combo2\" size=\"1\" class=\"WorkPageField\" id=\"Combo2\"> <option value=\"Yes\"". (($theValue == 'Yes') ? (' SELECTED') : ('') .">Yes <option value=\"No\"". (($theValue == 'No') ? (' SELECTED') : ('') .">No </select> "; ?> there needs to be a space between the rest of your option tags and 'SELECTED'. Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237763 Share on other sites More sharing options...
sanfly Posted April 25, 2007 Share Posted April 25, 2007 There was a space, directly after the "Yes" or "No" Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237767 Share on other sites More sharing options...
oceans Posted April 25, 2007 Author Share Posted April 25, 2007 SanFly, Initially I had a lot of problem with my server php MySQL, after 3 days of forums, I was instructed to add " <?php ob_start(); ?> " then the problem went away. My situation was the same then, forum's advisor had no problem, but I was dieing. Still no luck, I can't understand your solution boo_lolly. Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237771 Share on other sites More sharing options...
boo_lolly Posted April 25, 2007 Share Posted April 25, 2007 There was a space, directly after the "Yes" or "No" yes, but if one of them wasn't 'pre-selected', there would be a space between value="" and >. some browsers will react differently to this html syntax error. so, in my opinion, the space should be with the SELECTED. Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237774 Share on other sites More sharing options...
oceans Posted April 25, 2007 Author Share Posted April 25, 2007 Dear SanFly & Boo_Lolly Boo_Lolly's syntex appears different from mine, Is it ok if I ask for a reconstructed code, sorry if I am asking for too much. Just ammend SanFly's code, SanFly's code works on his PC but not in mine. Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237777 Share on other sites More sharing options...
oceans Posted April 25, 2007 Author Share Posted April 25, 2007 Dear people, Any Help for me Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237789 Share on other sites More sharing options...
benjaminbeazy Posted April 25, 2007 Share Posted April 25, 2007 assuming that //put this inside conditional if(isset($_POST..... $Combo2 = $_POST['Combo2']; <select name="Combo2" size="1" class="WorkPageField" id="Combo2"> <option value="Yes" <? if($Combo2 == "Yes"){ echo "selected"; } ?>>Yes</option> <option value="No" <? if($Combo2 == "No"){ echo "selected"; } ?>>No</option> </select> Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237800 Share on other sites More sharing options...
oceans Posted April 25, 2007 Author Share Posted April 25, 2007 Sorry Benjaminbeazy, Can you guide me little more, please. Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237814 Share on other sites More sharing options...
oceans Posted April 25, 2007 Author Share Posted April 25, 2007 OK I understood, I tried no difference, any alternative. Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237820 Share on other sites More sharing options...
benjaminbeazy Posted April 25, 2007 Share Posted April 25, 2007 whoops, i guess i lost u a lil bit... if you want these fields to be populated by vars set in the database, make sure that in the db the field values are listed as either yes or no, then query the db and generate an array of the row in question if you want these to be populated by posted content, i.e. after form is posted and form is returned, values are selected, use the other method i told u about both examples are in the code, just delete the commented lines above and below the code you wish to use <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <?PHP //Transfer Data from Screen to Memory $NumberOfTxtBoxes=5; $NumberOfTxtBoxesToBeFilled=5; $theValue = "No"; if (isset($_POST['Submit'])) { /* this grabs the variables from the posted form, if thats what you want $var1 = $_POST['var1']; $var2 = $_POST['var2']; */ /* this grabs the variables from a table $sql = "SELECT * from TABLE WHERE field='SOMETHING'"; $result = mysql_query($sql); $row = mysql_fetch_array($result); $var1 = $row['var1']; $var2 = $row['var2']; */ for ($i=1; $i<=$NumberOfTxtBoxes; $i++) { $InputFromScreen[$i]=strtoupper($_POST["Txt".$i]); } } else { for ($i=1; $i<=$NumberOfTxtBoxes; $i++) { $InputFromScreen[$i]=""; } } ?> <form id="form1" name="form1" method="post" action="Untitled-1.php"> <table width="200" border="1"> <tr> <td> </td> <td><input name="Txt1" value="<?PHP echo $InputFromScreen[1]; ?>" type="text" id="Txt1" /></td> </tr> <tr> <td> </td> <td><input name="Txt2" value="<?PHP echo $InputFromScreen[2]; ?>" type="text" id="Txt2" /></td> </tr> <tr> <td> </td> <td><input name="Txt3" value="<?PHP echo $InputFromScreen[3]; ?>" type="text" id="Txt3" /></td> </tr> <tr> <td> </td> <td><input name="Txt4" value="<?PHP echo $InputFromScreen[4]; ?>" type="text" id="Txt4" /></td> </tr> <tr> <td> </td> <td><input name="Txt33" value="<?PHP echo $InputFromScreen[5]; ?>" type="text" id="Txt33" /></td> </tr> <tr> <td> </td> <td><select name="var2" size="1" class="WorkPageField" id="var2"> <option value="Yes" <? if($var2 == "Yes"){ echo "selected"; } ?>>Yes</option> <option value="No" <? if($var2 == "No"){ echo "selected"; } ?>>No</option> </select></td> </tr> <tr> <td> </td> <td><select name="var1" size="1" class="WorkPageField" id="var2"> <option value="Yes" <? if($var1 == "Yes"){ echo "selected"; } ?>>Yes</option> <option value="No" <? if($var1 == "No"){ echo "selected"; } ?>>No</option> </select></td> </tr> <tr> <td> </td> <td><?PHP ?></td> </tr> <tr> <td> </td> <td><input type="submit" name="Submit" value="Submit" /></td> </tr> </table> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237821 Share on other sites More sharing options...
benjaminbeazy Posted April 25, 2007 Share Posted April 25, 2007 does that make sense? if not, please explain to me EXACTLY what you are trying to do Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237823 Share on other sites More sharing options...
oceans Posted April 25, 2007 Author Share Posted April 25, 2007 Thanks for your time, Please run this. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <?PHP //Transfer Data from Screen to Memory $NumberOfTxtBoxes=5; $NumberOfTxtBoxesToBeFilled=5; $var1 = "No"; $var2 = "No"; if (isset($_POST['Submit'])) { /* this grabs the variables from the posted form, if thats what you want $var1 = $_POST['var1']; $var2 = $_POST['var2']; */ /* this grabs the variables from a table $sql = "SELECT * from TABLE WHERE field='SOMETHING'"; $result = mysql_query($sql); $row = mysql_fetch_array($result); $var1 = $row['var1']; $var2 = $row['var2']; */ for ($i=1; $i<=$NumberOfTxtBoxes; $i++) { $InputFromScreen[$i]=strtoupper($_POST["Txt".$i]); } } else { for ($i=1; $i<=$NumberOfTxtBoxes; $i++) { $InputFromScreen[$i]=""; } } ?> <form id="form1" name="form1" method="post" action="Untitled-1.php"> <table width="200" border="1"> <tr> <td> </td> <td><input name="Txt1" value="<?PHP echo $InputFromScreen[1]; ?>" type="text" id="Txt1" /></td> </tr> <tr> <td> </td> <td><input name="Txt2" value="<?PHP echo $InputFromScreen[2]; ?>" type="text" id="Txt2" /></td> </tr> <tr> <td> </td> <td><input name="Txt3" value="<?PHP echo $InputFromScreen[3]; ?>" type="text" id="Txt3" /></td> </tr> <tr> <td> </td> <td><input name="Txt4" value="<?PHP echo $InputFromScreen[4]; ?>" type="text" id="Txt4" /></td> </tr> <tr> <td> </td> <td><input name="Txt33" value="<?PHP echo $InputFromScreen[5]; ?>" type="text" id="Txt33" /></td> </tr> <tr> <td> </td> <td><select name="var2" size="1" class="WorkPageField" id="var2"> <option value="Yes" <? if($var2 == "Yes"){ echo "selected"; } ?>>Yes</option> <option value="No" <? if($var2 == "No"){ echo "selected"; } ?>>No</option> </select></td> </tr> <tr> <td> </td> <td><select name="var1" size="1" class="WorkPageField" id="var2"> <option value="Yes" <? if($var1 == "Yes"){ echo "selected"; } ?>>Yes</option> <option value="No" <? if($var1 == "No"){ echo "selected"; } ?>>No</option> </select></td> </tr> <tr> <td> </td> <td><?PHP ?></td> </tr> <tr> <td> </td> <td><input type="submit" name="Submit" value="Submit" /></td> </tr> </table> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237830 Share on other sites More sharing options...
benjaminbeazy Posted April 25, 2007 Share Posted April 25, 2007 try this... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <?PHP //Transfer Data from Screen to Memory $NumberOfTxtBoxes=5; $NumberOfTxtBoxesToBeFilled=5; if (isset($_POST['Submit'])) { $var1 = $_POST['var1']; $var2 = $_POST['var2']; /* this grabs the variables from a table $sql = "SELECT * from TABLE WHERE field='SOMETHING'"; $result = mysql_query($sql); $row = mysql_fetch_array($result); $var1 = $row['var1']; $var2 = $row['var2']; */ for ($i=1; $i<=$NumberOfTxtBoxes; $i++) { $InputFromScreen[$i]=strtoupper($_POST["Txt".$i]); } } else { for ($i=1; $i<=$NumberOfTxtBoxes; $i++) { $InputFromScreen[$i]=""; } } ?> <form id="form1" name="form1" method="post" action=""> <table width="200" border="1"> <tr> <td> </td> <td><input name="Txt1" value="<?PHP echo $InputFromScreen[1]; ?>" type="text" id="Txt1" /></td> </tr> <tr> <td> </td> <td><input name="Txt2" value="<?PHP echo $InputFromScreen[2]; ?>" type="text" id="Txt2" /></td> </tr> <tr> <td> </td> <td><input name="Txt3" value="<?PHP echo $InputFromScreen[3]; ?>" type="text" id="Txt3" /></td> </tr> <tr> <td> </td> <td><input name="Txt4" value="<?PHP echo $InputFromScreen[4]; ?>" type="text" id="Txt4" /></td> </tr> <tr> <td> </td> <td><input name="Txt5" value="<?PHP echo $InputFromScreen[5]; ?>" type="text" id="Txt33" /></td> </tr> <tr> <td> </td> <td><select name="var2" size="1" class="WorkPageField" id="var2"> <option value="Yes" <? if($var2 == "Yes"){ echo "selected"; } ?>>Yes</option> <option value="No" <? if($var2 == "No"){ echo "selected"; } ?>>No</option> </select></td> </tr> <tr> <td> </td> <td><select name="var1" size="1" class="WorkPageField" id="var2"> <option value="Yes" <? if($var1 == "Yes"){ echo "selected"; } ?>>Yes</option> <option value="No" <? if($var1 == "No"){ echo "selected"; } ?>>No</option> </select></td> </tr> <tr> <td> </td> <td><?PHP ?></td> </tr> <tr> <td> </td> <td><input type="submit" name="Submit" value="Submit" /></td> </tr> </table> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/48431-solved-can-any-one-help-me-in-%E2%80%9Clistbox%E2%80%9D/#findComment-237832 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.