Dropdown select redirect

I want to redirect a user on submit based on the state page they select.

for example, they want a particular service in Colorado ad select “Colorado” upon clicking on the submit button they are taken to the Colorado page.

what I have so far


HTML
<form method="post" action="include/moveit.php">
		<div class="line"></div>
		<div class="bySt">
		<select name="St" class="state_select">
	<option selected="selected" value="">Browse by 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>

--------etc------

php code ; 

<?php
if (!empty($_REQUEST['States']))
{
    $url = array(
        'AL'=>'http://www.example.com/state/alabama.html',
        'AK'=>'http://www.example.com/state/arkansas.html',
        'AZ'=>'http://www.example.com/state/arizona.html',
        'AR'=>'http://www.example.com/state/arkansas.html',
        'CA'=>'http://www.example.com/state/california.html',
        'CO'=>'http://www.example.com/state/colorado.html',
......

else
{
     // No search query; redirect to search page
    header('Location:producers.html');
    die();
}
?>


I don’t want to put the url in the html code, I prefer to use the state abbreviation (less code heavy)

it just leads to a blank page right now

Any ideas, as I see many answers out there “Google searches” using javascript which I want to avoid in case user has it turned off and people do if they don’t want to see ads.

Thanks

You may wish to try this:


<?php
    $url = array(
        'AL'=>'http://www.example.com/state/alabama.html',
        'AK'=>'http://www.example.com/state/arkansas.html',
        'AZ'=>'http://www.example.com/state/arizona.html',
        'AR'=>'http://www.example.com/state/arkansas.html',
        'CA'=>'http://www.example.com/state/california.html',
        'CO'=>'http://www.example.com/state/colorado.html',
...);
if (!empty($_REQUEST['States'])) {
  $qs = $_SERVER['QUERY_STRING'];
  $url .= '?' . $qs;
  header("location: $url");
}
else
{
     // No search query; redirect to search page
    header('Location:producers.html');
   // No need for  die() here
}
?>

I am getting a syntax error in that code.

Have you left a trailing comma on the last array item? If so, you shouldn’t.

Please note that nearly ALL code posted here in answer to a question or problem is intended as an example only, and needs at least minor modification to make it functional for the querist’s particular application. This especially applies to code that I post, to assist someone. If used verbatim, that code will surely generate a syntax error, since there is an ellipsis (…) within the code. NEVER fully trust code you get from anywhere. You gotta read through it, at the very least. :smiley:

Edit:

It also seems that I made a minor goof on one line of that code, as well. This:


  $url .= '?' . $qs;
  header("location: $url");

should look more like this:


  $url = $_REQUEST['states'] . '?' . $qs;
  header("location: $url");

oops?

I appreciate the help.
I tried some alterations but came up short.

I know html and css but really know not very much in php (I’d be categorized a php 101 newbie).

We were all there, at one point, lukkas. Don’t worry. You’ll get there. do me a favor, and post that entire section of code you’re working on, and I’ll see what we can accomplish together. :slight_smile:


html section:
<form method="get" action="scripts/stateforms.php">
		<div class="line"></div>
		<div class="bySt">
		<select name="States" class="state_select">
	<option selected="selected" value="">Browse by 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>
	<option value="CT">Connecticut</option>
	<option value="DC">D.C.</option>
	<option value="DE">Delaware</option>
	<option value="FL">Florida</option>

	<option value="GA">Georgia</option>
	<option value="HI">Hawaii</option>
	<option value="ID">Idaho</option>
	<option value="IL">Illinois</option>
	<option value="IN">Indiana</option>
	<option value="IA">Iowa</option>
       <option value="WY">Wyoming</option>
    </select></div>
		<div class="line"></div>
		<div class="p">
		<input type="image" img src="images/button_findNow.gif" value="submit" alt="Find Surgeon" border="0" /></form>

------
<?php
if (!empty($_REQUEST['States']))
{
    $url = array(
        'AL'=>'http://www.example.com/alabama.html',
        'AK'=>'http://www.example.com/arkansas.html',
        'AZ'=>'http://www.example.com/arizona.html',
        'AR'=>'http://www.example.com/arkansas.html',
        'CA'=>'http://www.example.com/california.html',
        'CO'=>'http://www.example.com/colorado.html',
        'CT'=>'http://www.example.com/connecticut.html',
        'DC'=>'http://www.example.com/washington_dc.html',
        'DE'=>'http://www.example.com/delaware.html',
        'FL'=>'http://www.example.com/florida.html',
        'GA'=>'http://www.example.com/georgia.html',
        'HI'=>'http://www.example.com/hawaii.html',
        'ID'=>'http://www.example.com/idaho.html_',
        'IL'=>'http://www.example.com/illinois.html',
        'IN'=>'http://www.example.com/indiana.html',
        'IA'=>'http://www.example.com/iowa.html',
        'KS'=>'http://www.example.com/kansas.html',
        'KY'=>'http://www.example.com/kentucky.html',
        'LA'=>'http://www.example.com/louisiana.html',
        'ME'=>'http://www.example.com/maine.html',
        'MD'=>'http://www.example.com/maryland.html',
        'MA'=>'http://www.example.com/massachusetts.html',
        'MI'=>'http://www.example.com/michigan.html',
        'MN'=>'http://www.example.com/minnesota.html_',
        'MS'=>'http://www.example.com/mississippi.html',
        'MO'=>'http://www.example.com/missouri.html',
        'MT'=>'http://www.example.com/montana.html',
        'NE'=>'http://www.example.com/nebraska.html',
        'NV'=>'http://www.example.com/nevada.html',
        'NH'=>'http://www.example.com/new_hampshire.html',
        'NJ'=>'http://www.example.com/new_jersey.html',
        'NM'=>'http://www.example.com/new_mexico.html',
        'NY'=>'http://www.example.com/new_york.html',
        'NC'=>'http://www.example.com/north_carolina.html',
        'ND'=>'http://www.example.com/north_dakota.html',
        'OH'=>'http://www.example.com/ohio.html',
        'OK'=>'http://www.example.com/oklahoma.html',
        'OR'=>'http://www.example.com/oregon.html',
        'PA'=>'http://www.example.com/pennsylvania.html',
        'RI'=>'http://www.example.com/rhode_island.html',
        'SC'=>'http://www.example.com/south_carolina.html',
        'SD'=>'http://www.example.com/south_dakota.html',
        'TN'=>'http://www.example.com/tennessee.html',
        'TX'=>'http://www.example.com/texas.html',
        'UT'=>'http://www.example.com/utah.html',
        'VT'=>'http://www.example.com/vermont.html',
        'VA'=>'http://www.example.com/virginia.html',
        'WA'=>'http://www.example.com/washington.html',
        'WV'=>'http://www.example.com/west_virginia.html',
        'WI'=>'http://www.example.com/wisconsin.html',
        'WY'=>'http://www.example.com/wyoming.html');
if (!empty($_REQUEST['States'])) {
  $qs = $_SERVER['QUERY_STRING'];
  $url .= '?' . $qs;
  header("location: $url");
}
?>


This may be important. it is an html extension page not php but I already have the “addhandler” in the htaccess file.
This code is partly from stackoverflow so it is a cut & paste job but there has to be a viable way (no javascript)

The $url variable is an array, but you’re trying to use it as if it were a single string.

Try doing this instead:


$urls = array(
...
$state = $_REQUEST['States'];
$url = $urls[$state] . '?' . $qs;

sorry I don’t follow this goes in the beginning or end?
can you place the whole code in there?

Thanks


if (!empty($_REQUEST['States']))
{
    $urls = array(
        'AL'=>'http://www.example.com/alabama.html',
        'AK'=>'http://www.example.com/arkansas.html',
        'AZ'=>'http://www.example.com/arizona.html',
        'AR'=>'http://www.example.com/arkansas.html',
        'CA'=>'http://www.example.com/california.html',
        'CO'=>'http://www.example.com/colorado.html',
        'CT'=>'http://www.example.com/connecticut.html',
        'DC'=>'http://www.example.com/washington_dc.html',
        'DE'=>'http://www.example.com/delaware.html',
        'FL'=>'http://www.example.com/florida.html',
        'GA'=>'http://www.example.com/georgia.html',
        'HI'=>'http://www.example.com/hawaii.html',
        'ID'=>'http://www.example.com/idaho.html_',
        'IL'=>'http://www.example.com/illinois.html',
        'IN'=>'http://www.example.com/indiana.html',
        'IA'=>'http://www.example.com/iowa.html',
        'KS'=>'http://www.example.com/kansas.html',
        'KY'=>'http://www.example.com/kentucky.html',
        'LA'=>'http://www.example.com/louisiana.html',
        'ME'=>'http://www.example.com/maine.html',
        'MD'=>'http://www.example.com/maryland.html',
        'MA'=>'http://www.example.com/massachusetts.html',
        'MI'=>'http://www.example.com/michigan.html',
        'MN'=>'http://www.example.com/minnesota.html_',
        'MS'=>'http://www.example.com/mississippi.html',
        'MO'=>'http://www.example.com/missouri.html',
        'MT'=>'http://www.example.com/montana.html',
        'NE'=>'http://www.example.com/nebraska.html',
        'NV'=>'http://www.example.com/nevada.html',
        'NH'=>'http://www.example.com/new_hampshire.html',
        'NJ'=>'http://www.example.com/new_jersey.html',
        'NM'=>'http://www.example.com/new_mexico.html',
        'NY'=>'http://www.example.com/new_york.html',
        'NC'=>'http://www.example.com/north_carolina.html',
        'ND'=>'http://www.example.com/north_dakota.html',
        'OH'=>'http://www.example.com/ohio.html',
        'OK'=>'http://www.example.com/oklahoma.html',
        'OR'=>'http://www.example.com/oregon.html',
        'PA'=>'http://www.example.com/pennsylvania.html',
        'RI'=>'http://www.example.com/rhode_island.html',
        'SC'=>'http://www.example.com/south_carolina.html',
        'SD'=>'http://www.example.com/south_dakota.html',
        'TN'=>'http://www.example.com/tennessee.html',
        'TX'=>'http://www.example.com/texas.html',
        'UT'=>'http://www.example.com/utah.html',
        'VT'=>'http://www.example.com/vermont.html',
        'VA'=>'http://www.example.com/virginia.html',
        'WA'=>'http://www.example.com/washington.html',
        'WV'=>'http://www.example.com/west_virginia.html',
        'WI'=>'http://www.example.com/wisconsin.html',
        'WY'=>'http://www.example.com/wyoming.html');
if (!empty($_REQUEST['States'])) {
  $qs = $_SERVER['QUERY_STRING'];
  $state = $_REQUEST['States'];
  $url = $urls[$state] . '?' . $qs;
  header("location: $url");
}

Well, I don’t want the thread to end up a mile long, but this should be enough to indicate proper placement. :slight_smile:


        'WA'=>'http://www.example.com/washington.html',
        'WV'=>'http://www.example.com/west_virginia.html',
        'WI'=>'http://www.example.com/wisconsin.html',
        'WY'=>'http://www.example.com/wyoming.html');
if (!empty($_REQUEST['States'])) {
  $state = $_REQUEST['States'];
  $qs = $_SERVER['QUERY_STRING'];
  $url = $urls[$state] . '?' . $qs;
  header("location: $url");
}
?>

you beat me to the punch, Paul! :lol:

Great !! so that is the full php file needed.

Although, since it says “query” am I required to have a MySql database?

I will test it soon .

Thanks

well, it didn’t work for me.

I used:


<form method="get" action="redirect.php">
<select name="States" class="state_select">
	<option selected="selected" value="">Browse by 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>
............
<input type="submit" value="Submit" name="submit"></div>
	</form>



along with the code you suggested called redirect.php

the result is a blank page. I can normally put 2 & 2 together but this one is not adding up for some reason.

You may want to check your error logs, to make sure there’s not some sort of problem going on with the header statement. It could be that you’re experiencing the “whitespace syndrome”.

The header location part works only if nothing has been written to the page yet.

You can confirm that the script has put together the correct url by placing this line just after the $url line.


var_dump($url);

If the url isn’t being displayed, check before the if statement that you have the ‘States’ value by dumping the $_GET variable.

If the url is being displayed, you can remove the var_dump line. The url using being put together correctly so it’s most likely that some other content is being written to the page before the header() command has been processed.

I get this error:

PHP Parse error: syntax error, unexpected $end in /…/redirect.php on line 66

but line 66 is not even used it ends at 64 with $>

and yes that whitespace syndrome is happening.

note: maybe it makes a difference but all html pages are php server parsed as this select menu is on each page.

and also happy new year

I suggest that you need to show us the code that you’re using for redirect.php

Please don’t refer us to the above code. Instead, copy/paste the actual code that you are actually using.

It seems that there is an extra closing curly brace at the end of your script. So you must post the whole code in that file not only certain lines around the error has occurred.