PHP, Javascript and WordPress problem
-
I’m brand new with PHP and now had to jump into the ocean with no swimming lessons. So I appreciate the help! I’m having a problem with a PHP page on our WordPress site.
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:
All the tutorials say the best way to do this is with a form reload on a Javascript onchange event. But…. WordPress 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 since that’s not the page.
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><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><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 = “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!”);$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>
<input type=”submit” name=”mysubmit” value=”Submit Form” />
</form>
</div>
</div><?php get_sidebar(); ?>
<?php get_footer(); ?>Thank you for your help!
- The topic ‘PHP, Javascript and WordPress problem’ is closed to new replies.