eddlandos Posted June 15, 2011 Share Posted June 15, 2011 I've been trying to get forms to show the existing values from the database is there a more effective way of doing this? Sexuality <br /> <select name="txtSexuality"> <option value="Straight">Straight</option> <option value="Bisexual">Bisexual</option> <option value="Open-Minded">Open-Minded</option> <option value="Gay/Lesbian">Gay/Lesbian</option> <option selected <? showSexuality();?></option> </select> <br /> Quote Link to comment Share on other sites More sharing options...
cssfreakie Posted June 15, 2011 Share Posted June 15, 2011 I happen to have written something today, The input used is an array, so you can easily use it for database values. <?php error_reporting(E_ALL); ini_set("display_errors", 1); header('Content-type: text/html; charset=utf-8'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link type="text/css" rel="stylesheet" href="csfs/style.css" /> <title></title> </head> <body> <div id="wrapper"> <?php $var = array( '1.1'=>'moo', '1.2'=>'maa', '1.3'=>'boo', '1.4'=>'baa', '1.5'=>'zaa'); function BuildSelect($var, $name, $size){ echo '<select name="'.$name.'" size="'.$size.'">'; foreach ($var as $key=>$value) { $s = ''; if (isset($_POST[$name])&& $key == $_POST[$name]) { $s = 'selected="selected"'; } echo '<option value="' . $key . '" ' . $s . '>' . $value . '</option>'; } echo '</select>'; } ?> <form action="" method="post"> <?php BuildSelect($var, 'moeloe', 1 ) ?> <input type="submit" name="submit" value="submit" /> </form> </div> </body> </html> p.s. normaly you would seperate the php from the html by using an include, but now you can just test it at once Quote Link to comment Share on other sites More sharing options...
redixx Posted June 15, 2011 Share Posted June 15, 2011 What does showSexuality() return? This is how I usually do it: // value => display $options = array( 'Straight' => 'Straight', 'Bisexual' => 'Bisexual', 'Open-Minded' => 'Open-Minded', 'Gay/Lesbian' => 'Gay/Lesbian' ); // this is the value of the selected // however you want to get that value $sexuality = 'Straight'; echo '<select name="txtSexuality">'; foreach ($options as $key=>$val) { $selected = ($sexuality == $key) ? 'selected="selected"' : ''; echo '<option value="' . $key . '" ' . $selected . '>' . $val . '</option>'; } echo '</select>'; Quote Link to comment Share on other sites More sharing options...
cssfreakie Posted June 15, 2011 Share Posted June 15, 2011 is don't see a function named showSexuality() in your code above. Your code just provides a drop down based on an array. Although i am pretty sure that this: $selected = ($sexuality == $key) ? 'selected="selected"' : ''; will produced an error on first run since there is no value assigned to $sexuality -edit: lol, i didn't noticed above are 2 different peeps lol Quote Link to comment Share on other sites More sharing options...
eddlandos Posted June 15, 2011 Author Share Posted June 15, 2011 function showSexuality() { global $sexuality; echo 'value="'. $sexuality . '">'. $sexuality . ''; } is what's in the function I'm guessing I really need a boolean to say selected ? Quote Link to comment Share on other sites More sharing options...
cssfreakie Posted June 15, 2011 Share Posted June 15, 2011 Did you try out the codes above? if so look at this piece: foreach ($var as $key=>$value) { $s = ''; if (isset($_POST[$name])&& $key == $_POST[$name]) { $s = 'selected="selected"'; } echo '<option value="' . $key . '" ' . $s . '>' . $value . '</option>'; } What it does is check for each array value if $_POST['name'] is set, and if so compares the value of the array key with the value of $_POST['name']. IF that condition is true it means the form is submitted and that that value is the one someone selected. making it output a different string than it does by default. So in other words it will remember what the person selected. Quote Link to comment Share on other sites More sharing options...
redixx Posted June 15, 2011 Share Posted June 15, 2011 is don't see a function named showSexuality() in your code above. Your code just provides a drop down based on an array. Although i am pretty sure that this: $selected = ($sexuality == $key) ? 'selected="selected"' : ''; will produced an error on first run since there is no value assigned to $sexuality I assumed that's what he wanted. And $sexuality is assigned, right here: $sexuality = 'Straight'; I just put it to "Straight" for an example, but you could just as easily do $sexuality = $_POST['sexuality'], or get the value from a database. Quote Link to comment Share on other sites More sharing options...
cssfreakie Posted June 15, 2011 Share Posted June 15, 2011 @redixx Ah i missed that part where you assigned it I stand corrected And using the ternary operator, even looks cooler. Quote Link to comment Share on other sites More sharing options...
eddlandos Posted June 19, 2011 Author Share Posted June 19, 2011 managed to suss your code out thanks very much guys Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.