Form not passing field value
Posted: Fri Feb 24, 2012 8:59 am
This should be so basic; I've done it thousands of times. I have a form, the user selects the value, and I grab that value on the next page with POST. But it's blank, and I must be staring at this for too long because I can't see why.
It's a form that fills the first select box from a state array; the second dropdown pulls from a database based on the state. When I look at the output from the code, it's fine. Here it is in action: http://youradminpartners.com/state-selection/. It's not pretty yet, because I'm focusing on function. Form code:
When I get to the next page, I echo the variable to see if it's there, but it's blank.
Thanks for your help!
It's a form that fills the first select box from a state array; the second dropdown pulls from a database based on the state. When I look at the output from the code, it's fine. Here it is in action: http://youradminpartners.com/state-selection/. It's not pretty yet, because I'm focusing on function. Form code:
Code: Select all
<form action="http://youradminpartners.com/bcompliant/" method="post">
<?php
$state=$_GET['state'];
function showOptionsDrop($array,$state)
{
$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="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">Select state</option>
<?php echo showOptionsDrop($states_arr,$state); ?>
</select>
<br /><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[name]\">$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="Continue to Public Portal" /> <button type="button" style="width:65;height:65" onClick="window.location='http://youradminpartners.com/state-selection/'">Clear State</button>
</form>Thanks for your help!