Using a variable in an array, when variable is pulled from an sql table

strovocosvo

New Member
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)\[code\] 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="http://stackoverflow.com/questions/3622935/ebf" /></p> <p><input type="submit" name="submit" value="http://stackoverflow.com/questions/3622935/Update Contact" /></p> </form>'; echo "$editBlock"; }\[/code\]
 
Back
Top