PHP dynamic population problem

eblackwell
10:10:11
I'm brand new with PHP and have been thrown into the pool with no swimming lessons, so I appreciate the tutorials!

I am trying to populate a second dropdown with database values based on the first one. I have a couple problems, but let me deal with the first one:

This is on a Wordpress site; WP is changing names for the site by stripping extensions and adding a trailing "/". For example, my page is named state_selection.php. Wordpress turns the address to /state-selection/.

When I reload the page on the form reload, my value is like this: /state-selection/?state=VA. It comes back as Error 404.

I know I have other issues since the Submit button doesn't show on the first load of the page. Here is my code:

The JS script is in the header.php file:
<SCRIPT language=JavaScript>
function reload(form){
var val=form.state.options[form.state.options.selectedIndex].value;
self.location='state_selection.php?state=' + val ;
}
</script>

I have tried
self.location='state-selection/?state=' + val ;

But I still get the Error 404.

Is there any way to pass the state value without a reload form?

BTW, I have done the state select as an array, and when that didn't work, I did a straight HTML box. I abbreviated it here for sake of space.

Here is the main page code:
<?php /* Template Name: State and Employer Selection*/ ?>

<?php get_header(); ?>

<div id="main_content">
<div class="stateemp">
<p>PLEASE SELECT YOUR STATE IN THE DROP DOWN BOX BELOW:</p>
<br /><br />
<form action="state_selection.php" method="post" name="state">
<select name="state" onchange="reload(this.form)">
<option value="" selected="selected">Select state</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
</select>

<br /><br />

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

<select name='employer'>
<option value=''>Choose Employer</option>

<?php

// set database server access variables:
$host = "localhost";
$user = "";
$pass = "";
$db = "";

// 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!");

$state=$_GET['state'];

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

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

// 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>";
}


// close connection
mysql_close($connection);


?>

</select>
<br /><br />
<input type="submit" name="mysubmit" value="Submit Form" />
</form>
</div>
</div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>


THank you for your help!
smo1234
10-16-2011
Not understand how to modify the WP problem but if want to pass the form value without reloading then read this Ajax PHP Drop down list box

(Do not expose your database userid and password in your posts here ) .
eblackwell
10-17-2011
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).

<?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>
smo1234
10-17-2011
This should help FAQ
snhlawy
02-23-2015
i have the below error when trying to read arabic charaters from DB to be displayed in the dropdown menu but if i read english charaters it works fine please help. the error message is

SCREAM: Error suppression ignored for
( ! ) Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\Autoservice website & system\inventory\pqty.php on line 132

my code is

<select name="owner">
<?php
mysql_connect("localhost","snhlawy","thena7labee") or die("Couldn't connect.");
mysql_select_db("data") or die ("Couldn't select db.");
$sql = mysql_query("SELECT desc FROM inventory");
while ($row = mysql_fetch_array($sql)){

?>
<option value="owner1"><?php echo $row['desc']; ?></option>

<?php
// close while loop
}
?>
Please Login to post your reply or start a new topic