Jump to content

Form with options


harkly

Recommended Posts

I have a form that has a drop down, it updates the database with a number and then when that user calls the form again it will have their option already selected.

 

What I am wondering is if there is a more efficient way of doing this? another of my drop downs has more than 30 options. I will need to change to another format for time issues, at least I think.

 

                 if ($education == 2){
                   echo "
                         <select name='education' size='1' id='st_age' class='selectOne'>
                   <option value='1'></option>
                   <option value='2' selected='selected'>Some High school</option>
                   <option value='3'>High school/GED</option>
                   <option value='4'>Some college</option>
                   <option value='5'>Associates</option>
                   <option value='6'>Bachelors</option>
                   <option value='7'>Masters</option>
                   <option value='8'>PhD / Post Doctoral</option>
                         </select> \n";}

                else if ($education == 3){
                   echo "
                         <select name='education' size='1' id='st_age' class='selectOne'>
                   <option value='1'></option>
                   <option value='2'>Some High school</option>
                   <option value='3' selected='selected'>High school/GED</option>
                   <option value='4'>Some college</option>
                   <option value='5'>Associates</option>
                   <option value='6'>Bachelors</option>
                   <option value='7'>Masters</option>
                   <option value='8'>PhD / Post Doctoral</option>
                         </select> \n";}

                 else if ($education == 4){
                   echo "
                         <select name='education' size='1' id='st_age' class='selectOne'>
                   <option value='1'></option>
                   <option value='2'>Some High school</option>
                   <option value='3'>High school/GED</option>
                   <option value='4' selected='selected'>Some college</option>
                   <option value='5'>Associates</option>
                   <option value='6'>Bachelors</option>
                   <option value='7'>Masters</option>
                   <option value='8'>PhD / Post Doctoral</option>
                         </select> \n";}

                 else if ($education == 5){
                   echo "
                         <select name='education' size='1' id='st_age' class='selectOne'>
                   <option value='1'></option>
                   <option value='2'>Some High school</option>
                   <option value='3'>High school/GED</option>
                   <option value='4'>Some college</option>
                   <option value='5' selected='selected'>Associates</option>
                   <option value='6'>Bachelors</option>
                   <option value='7'>Masters</option>
                   <option value='8'>PhD / Post Doctoral</option>
                         </select> \n";}

                 else if ($education == 6){
                   echo "
                         <select name='education' size='1' id='st_age' class='selectOne'>
                   <option value='1'></option>
                   <option value='2'>Some High school</option>
                   <option value='3'>High school/GED</option>
                   <option value='4'>Some college</option>
                   <option value='5'>Associates</option>
                   <option value='6' selected='selected'>Bachelors</option>
                   <option value='7'>Masters</option>
                   <option value='8'>PhD / Post Doctoral</option>
                       </select> \n";}

                 else if ($education == 7){
                   echo "
                         <select name='education' size='1' id='st_age' class='selectOne'>
                   <option value='1'></option>
                   <option value='2'>Some High school</option>
                   <option value='3'>High school/GED</option>
                   <option value='4'>Some college</option>
                   <option value='5'>Associates</option>
                   <option value='6'>Bachelors</option>
                   <option value='7' selected='selected'>Masters</option>
                   <option value='8'>PhD / Post Doctoral</option>
                         </select> \n";}

                 else if ($education == {
                   echo "
                         <select name='education' size='1' id='st_age' class='selectOne'>
                   <option value='1'></option>
                   <option value='2'>Some High school</option>
                   <option value='3'>High school/GED</option>
                   <option value='4'>Some college</option>
                   <option value='5'>Associates</option>
                   <option value='6'>Bachelors</option>
                   <option value='7'>Masters</option>
                   <option value='8' selected='selected'>PhD / Post Doctoral</option>
                         </select> \n";}

                 else if ($education == 1){
                   echo "
                         <select name='education' size='1' id='st_age' class='selectOne'>
                   <option value='1' selected='selected'></option>
                   <option value='2'>Some High school</option>
                   <option value='3'>High school/GED</option>
                   <option value='4'>Some college</option>
                   <option value='5'>Associates</option>
                   <option value='6'>Bachelors</option>
                   <option value='7'>Masters</option>
                   <option value='8'>PhD / Post Doctoral</option>
                         </select> \n";}

                 else {
                   echo "
                         <select name='education' size='1' id='st_age' class='selectOne'>
                   <option value='1' selected='selected'></option>
      	                   <option value='2'>Some High school</option>
                   <option value='3'>High school/GED</option>
                   <option value='4'>Some college</option>
                   <option value='5'>Associates</option>
                   <option value='6'>Bachelors</option>
                   <option value='7'>Masters</option>
                   <option value='8'>PhD / Post Doctoral</option>
                         </select> \n";}
                   echo "

Link to comment
https://forums.phpfreaks.com/topic/183467-form-with-options/
Share on other sites

something like:

 

<?php
$edu_arr = array (
1 => 'Select One',
2 => 'Some High school',
3 => 'High school/GED',
4 => 'Some college',
5 => 'Associates',
6 => 'Bachelors',
7 => 'Masters',
8 => 'PhD / Post Doctoral'
);

echo '<select name="education" size="1" id="st_age" class="selectOne">';
foreach ($edu_arr as $key => $value)
{
echo '<option value="'.$key.'"'.(($key == $education) ? ' selected="selected"' : '').'>'.$value.'</option>'; }
}
echo '</select>';
?>

 

EDIT: you can swap out the value 'Select One' and have an empty value '', or you can change it to whatever you like.

Link to comment
https://forums.phpfreaks.com/topic/183467-form-with-options/#findComment-968442
Share on other sites

I can't seem to get anything but a blank page. Right now my $education = 3

 

<?php
   include('library/login.php');
   login();

  mysql_select_db('bariatric'); 
  $search=$_GET['username'];

$result = mysql_query("SELECT * FROM about_me WHERE username = 'kelly'");

  while ($r=mysql_fetch_array($result))
   {

   $username=$r["username"];
   $education=$r["education"];

}



$edu_arr = array (
1 => 'Select One',
2 => 'Some High school',
3 => 'High school/GED',
4 => 'Some college',
5 => 'Associates',
6 => 'Bachelors',
7 => 'Masters',
8 => 'PhD / Post Doctoral'
);

echo " <select name='education' size='1' id='st_age' class='selectOne'>";

foreach ($edu_arr as $key => $value)

{
echo " <option value="'.$key.'"'.(($key == $education) ? ' selected="selected"' : '').'>'.$value.'</option>'"; 
}

echo "</select>";



?>

 

Am I missing something outside of the array??

Link to comment
https://forums.phpfreaks.com/topic/183467-form-with-options/#findComment-969398
Share on other sites

Whew!  I was able to get it to work so that it will display what is currently in the database but it wont update the database when something else is choosen. Can anyone help me on that?

 

<?php
   include('library/login.php');
   login();

  mysql_select_db('database'); 
  $search=$_GET['username'];

$result = mysql_query("SELECT * FROM about_me WHERE username = 'test'");

  while ($r=mysql_fetch_array($result))
   {
     $username=$r["username"];
     $education=$r["education"];
   }

$states = array(
1 => '',
2 => 'Some High school',
3 => 'High school/GED',
4 => 'Some college',
5 => 'Associates',
6 => 'Bachelors',
7 => 'Masters',
8 => 'PhD / Post Doctoral'
);

$select = '<select name="state" id="state" size="1">'."\r\n";

foreach($states as $key => $value){
    $select .= "\t".'<option value="'.$key.'"'.(($key == $education) ? ' selected="selected"' : '').'>'. $value.'</option>'."\r\n";
}

$select .= '</select>';

echo $select;

?>

Link to comment
https://forums.phpfreaks.com/topic/183467-form-with-options/#findComment-969410
Share on other sites

Yes, its within the form. Here is a full version.

 

<!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'>
<head>
<link rel='stylesheet' type='text/css' href='app.css'>
<link href='wforms-jsonly.css' type='text/css' rel='alternate stylesheet' title='this stylesheet is activated by javascript' />
<script type='text/javascript' src='wforms.js' ></script>
</head>

<body>

<?php
   include('library/login.php');
   login();
?>

   <div id='wrapperLifestyle'>
     <div id='header'><a href='index.html'><img src='img/redLogo.gif' border='0'></a></div>

       <div id='aboutLifestyle'>
         <span class='right'>

<?php
  mysql_select_db('bariatric'); 
  $search=$_GET['username'];

$result = mysql_query("SELECT * FROM about_me WHERE username = 'kelly'");

  while ($r=mysql_fetch_array($result))
   {

   $username=$r["username"];
   $education=$r["education"];

   }

   echo "
      <form method='post' action='updateAboutMe.php' name='test' id='test'>

         <fieldset>
           <div id='aboutForm'>
             <span class='headerbox'><b>Education level:</b></span>
             <span class='textbox'>
               \n";
                   $states = array(
                     1 => '',
                     2 => 'Some High school',
                     3 => 'High school/GED',
                     4 => 'Some college',
                     5 => 'Associates',
                     6 => 'Bachelors',
                     7 => 'Masters',
                     8 => 'PhD / Post Doctoral'
                   );

                   $select = '<select name="state" id="state" size="1">'."\r\n";

                   foreach($states as $key => $value){
                       $select .= "\t".'<option value="'.$key.'"'.(($key == $education) ? ' selected="selected"' : '').'>'. $value.'</option>'."\r\n";
                   }

                   $select .= '</select>';

                     echo $select;

                   echo "

             </span>
           </div>
         </fieldset>


         <fieldset>
           <div id='aboutForm'>
             <span class='button'>
               <input type='image' value='Update' src='img/button_update.gif' width='75' height='25'
               onmouseover='javascript:this.src='img/button_update2.gif';' onmouseout='javascript:this.src='img/button_update.gif';'> 
             </span>
           </div>
         <fieldset>
             </form>
           </div>
         </span>

</form>
\n";

?>


</body></html>

 

 

Is this line passing the key into the value? because I think that is what I need

 

 $select .= "\t".'<option value="'.$key.'"'.(($key == $education) ? ' selected="selected"' : '').'>'. $value.'</option>'."\r\n";

Link to comment
https://forums.phpfreaks.com/topic/183467-form-with-options/#findComment-969431
Share on other sites

Got it to work!!

 

                   $states = array(
                     1 => '',
                     2 => 'Some High school',
                     3 => 'High school/GED',
                     4 => 'Some college',
                     5 => 'Associates',
                     6 => 'Bachelors',
                     7 => 'Masters',
                     8 => 'PhD / Post Doctoral'
                   );

                   $select = '<select name="education" id="st_age" size="1" class="selectOne">'."\r\n";

                   foreach($states as $key => $value){
                       $select .= "\t".'<option value="'.$key.'"'.(($key == $education) ? ' selected="selected"' : '').'>'. $value.'</option>'."\r\n";
                   }

                   $select .= '</select>';

                     echo $select;

Link to comment
https://forums.phpfreaks.com/topic/183467-form-with-options/#findComment-969450
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.