Problem with setting a form dropdown

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eblackwell
Forum Newbie
Posts: 19
Joined: Mon Oct 10, 2011 3:19 pm

Problem with setting a form dropdown

Post by eblackwell »

I have 2 dropdowns; the second pulls from a database based on the value of the first. Everything works fine, except the selected value doesn't get set when the page is reloaded.

I got the WP problem fixed and the form works fine. Only one hiccup:

When the form reloads, the selected value doesn't get set. Here's the code (minus the Javascript).

Code: Select all

<?php
$states_arr = array('AL'=>"Alabama",'AK'=>"Alaska",'AR'=>"Arkansas",'AZ'=>"Arizona",'CA'=>"California",'CO'=>"Colorado",'CT'=>"Connecticut",'DC'=>"District Of Columbia",'DE'=>"Delaware",'FL'=>"Florida",'GA'=>"Georgia",'HI'=>"Hawaii",'IA'=>"Iowa",'ID'=>"Idaho",'IL'=>"Illinois", 'IN'=>"Indiana",'KS'=>"Kansas",'KY'=>"Kentucky",'LA'=>"Louisiana",'MA'=>"Massachusetts",'MD'=>"Maryland",'ME'=>"Maine",'MI'=>"Michigan",'MN'=>"Minnesota",'MO'=>"Missouri",'MS'=>"Mississippi",'MT'=>"Montana",'NC'=>"North Carolina",'ND'=>"North Dakota",'NE'=>"Nebraska",'NH'=>"New Hampshire",'NJ'=>"New Jersey",'NM'=>"New Mexico",'NV'=>"Nevada",'NY'=>"New York",'OH'=>"Ohio",'OK'=>"Oklahoma", 'OR'=>"Oregon",'PA'=>"Pennsylvania",'RI'=>"Rhode Island",'SC'=>"South Carolina",'SD'=>"South Dakota",'TN'=>"Tennessee",'TX'=>"Texas",'UT'=>"Utah",'VA'=>"Virginia",'VT'=>"Vermont",'WA'=>"Washington",'WI'=>"Wisconsin",'WV'=>"West Virginia",'WY'=>"Wyoming");
?>

<div class="title" class="post" id="post-">

<h1>ADMIN Partners third party 403b plan administrator, experienced, professional</h1>
<h2></h2>
<br /><br />
<img src="/wp-content/themes/adminpartners/images/building_ext_rear_1.jpg" />
</div>

<div id="main_content">
<p>Start by entering your plan portal by selecting your state and employer. You'll get your Plan Summary, Calendar, News, and access to your Private pages with all plan information.</p>

<br />
<div class="stateemp">
<p>PLEASE SELECT YOUR STATE IN THE DROP DOWN BOX BELOW:</p>

<form action="state_selection.php" method="post" name="state">
<?php

$state=$_GET['state'];

function showOptionsDrop($array)
{
$string = '';
foreach($array as $k => $v)
{
// check to see if state has already been selected
// k equals the state abbreviation code

if(isset($state) and strlen($state) > 0)
{
if ($state==$k)
{
$string .= '<option selected value="'.$k.'">'.$v.'</option>'."\n";
}
} else {
$string .= '<option value="'.$k.'">'.$v.'</option>'."\n";
}
}
return $string;
}
?>

<select name="state" onchange="reload(this.form)">
<option value="0">Choose your state</option>
<?php echo showOptionsDrop($states_arr); ?>
</select>

<br /><br />

<p>PLEASE SELECT YOUR EMPLOYER'S NAME:</p>

<select name='employer'>
<option value=''>Select employer</option>
<?php
// set database server access variables:
$host = "localhost";
$user = "youradmi";
$pass = "Admin8Partners";
$db = "youradmi_demographics";

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

if(isset($state) and strlen($state) > 0)
{
// create query
$query="SELECT DISTINCT name FROM employer_state where state='$state' order by name";
}
else
{
// create query
$query="SELECT DISTINCT name FROM employer_state order by name";
}

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned
if (mysql_num_rows($result) > 0)
{
// yes
// printing the list box select command
while($nt=mysql_fetch_array($result))
{
//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[name]</option>";
/* Option values are added by looping through the array */
}
}
else
{
// no
echo "<option value=''>No employers in that state</option>";
}
// close connection
mysql_close($connection);

?>
</select>
<br /><br />
<input type="submit" name="mysubmit" value="Submit Form" />
</form>
</div>
</div>
Last edited by Benjamin on Mon Oct 17, 2011 3:09 pm, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
ouchiko
Forum Commoner
Posts: 35
Joined: Sun Oct 09, 2011 6:54 pm
Location: London

Re: Problem with setting a form dropdown

Post by ouchiko »

You set state as a variable outside the function - since you dont pass it through to the function it's always blank.

send through $state, showOptionsDrop($array,$state);
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Problem with setting a form dropdown

Post by califdon »

And please, for future reference, when you post a lot of code lines like that, use the PHP Code button to format your code so that it is readable. Thank you.
eblackwell
Forum Newbie
Posts: 19
Joined: Mon Oct 10, 2011 3:19 pm

Re: Problem with setting a form dropdown

Post by eblackwell »

I had family issues or I'd have replied sooner. I did what you said -- makes sense -- but it didn't work.

??

Erin
ouchiko wrote:You set state as a variable outside the function - since you dont pass it through to the function it's always blank.

send through $state, showOptionsDrop($array,$state);
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Problem with setting a form dropdown

Post by Celauran »

Did you remember to change both the function signature and the function call?
eblackwell
Forum Newbie
Posts: 19
Joined: Mon Oct 10, 2011 3:19 pm

Re: Problem with setting a form dropdown

Post by eblackwell »

D'oh!!

I knew I did something stupid! Thanks! Both for the answer and the patience!
Post Reply