Jump to content

Form Dropdowns


eddlandos

Recommended Posts

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 />

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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>';

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.