tags:

views:

28

answers:

3

Basically I am trying to make a simple form that allows the user to edit data in a table. It pulls the data from the database and uses those variables to prefill the form. Then the user can just edit the info in the form and resubmit the data. The problem is... the state selection is a function that auto creates the drop down box in the form filling it with all the states. I figured if I just add a variable to the beginning of that state array, then the first option in the array will be the state that was already in the database, but it does not work - it just comes up blank. (the first option shown in the drop down is blank, the rest of the list of states do show as it should)

 while ($row = mysql_fetch_array($result)) {
      $id = $row['id'];
      $first = $row['first'];
      $last = $row['last'];
      $city = $row['city'];
      $state = $row['state'];
      $email = $row['email'];
      $bday = $row['bday'];

        function state_selection()
        {
            $str = '<select name="state2">';
            $states = array(
                    "$state","Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii",                                                                                                   "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri",                                                                            "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma",                                                                            "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia",                                                                            "Wisconsin", "Wyoming"
                );
            foreach ($states as $state_name)
            {
                $str .= '<option value="'.$state_name.'">'.$state_name.'</option>';
            }
            $str .= '</select>';

            return $str;
        }

      $editBlock = '<form method="post" action="./sadd.php">
      <p>First Name: <input name="first2" value="' . $first . '" type="text"  size=13 maxlength=25 /></p>
      <p>Last name: <input name="last2" value="' . $last . '" type="text" size=13 maxlength=25 /></p>
      <p>City:<input name="city2" value="' . $city . '" type="text" size=25 maxlength=50 /></p>
      <p>State: '.state_selection().' </p>
      <p>Email:<input name="email2" value="' . $email . '" type="text" size=50 maxlength=75 /></p>
      <p>Birthday:<input name="bday2" value="' . $bday . '" type="text" size=10 maxlength=10 />(ex 1982-06-26)</p>
      <p><input type="hidden" name="eb" value="ebf" /></p>
      <p><input type="submit" name="submit" value="Update Contact" /></p>
      </form>';

      echo "$editBlock"; }
A: 

You cannot access the variable $state in the function without declaring it as global. Add this line to the beginning of state_selection()

global $state;

Another option would be to pass in $state as a parameter of state_selection()

Luke
+2  A: 

$state is not in scope, alter to

 function state_selection($state){

And call like

 state_selection($state);

A more reliable method instead of prepending would be

'<option value="'.$state_name.'" '.($state_name==$state)?'selected':'').'>'
Wrikken
A: 

Here is some updated code. A few key notes:

  • You do not need to loop if you are only pulling 1 record.
  • The array inside the function is unnecessary. I do not like globals, so I set it to pass as a parameter.
  • Added a default parameter, this will set the state selected to that.
  • Removed the assigning the array values to a variable on it's own. This is not necessary and inefficient. Better to access them through the array.

Any questions let me know.

function state_selection($states, $default) {
    $str = '<select name="state2">';

    foreach ($states as $state_name) {
        $str .= '<option value="'.$state_name.'" ' . (($default == $state_name)?"selected":"") . '>'.$state_name.'</option>';
    }

    $str .= '</select>';

    return $str;
}

$states = array("Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", 
                    "Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", 
                    "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", 
                    "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", 
                    "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", 
                    "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", 
                    "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming");

$row = mysql_fetch_assoc($result);

$editBlock = '<form method="post" action="./sadd.php"> 
      <p>First Name: <input name="first2" value="' . $row['first'] . '" type="text"  size=13 maxlength=25 /></p>
      <p>Last name: <input name="last2" value="' . $row['last'] . '" type="text" size=13 maxlength=25 /></p>
      <p>City:<input name="city2" value="' . $row['city'] . '" type="text" size=25 maxlength=50 /></p>
      <p>State: ' . state_select($states, $row['state']) . ' </p>
      <p>Email:<input name="email2" value="' . $row['email'] . '" type="text" size=50 maxlength=75 /></p>
      <p>Birthday:<input name="bday2" value="' . $row['bday'] . '" type="text" size=10 maxlength=10 />(ex 1982-06-26)</p>
      <p><input type="hidden" name="eb" value="ebf" /></p>
      <p><input type="submit" name="submit" value="Update Contact" /></p>
      </form>';

echo $editBlock; 
Brad F Jacobs