Forum Moderators: coopster

Message Too Old, No Replies

PHP noob - forms and redirects

Trying to take a submitted field and redirect...

         

Terabytes

2:15 pm on Jul 26, 2010 (gmt 0)

10+ Year Member



I'm "just" learning php due to a request at work.

I need to take a form field, and redirect to a specific page. I also need to submit that form data to a mail handler. (click 'submit' and accomplish both tasks)

Here's where I'm at (in the simplest terms)


<html>
<form action='fomr_handler.php' method='POST'>
<input type='text' name='name'>
<input type='text' name='company'>
<input type='text' name='address'>
<input type='text' name='city'>
<input type='text' name='state'>
<input type='text' name='zip'>
<input type='submit' value='Click'>
</form>
</html>

<?php

$name = $_POST['name'];
$name = $_POST['company'];
$name = $_POST['address'];
$name = $_POST['city'];
$name = $_POST['state'];
$name = $_POST['zip'];

?>


I'd like to redirect based upon the state (I.E. "texas.htm")

If anyone can shed some light on this, or direct me to a resource to accomplish this task I'd be grateful!

I've been looking at several other places, but since this is new to me, I'm having trouble getting my head around it..

thanks in advance!
Tera

rocknbil

4:52 pm on Jul 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well first, guessing this is just a quick post, but note that $name will only contain the last value, be sure those are set as individual variables.

You will need some sort of lookup table, and also need to make sure your state is a select list so you can rely on the values. You can put it in a database, or hard code it like so, in an array:

$states = Array(
// other states here
'TX' => 'texas.html',
// more states
);

Then do

header("location:$states[$input['state']]");

Where $input is your cleansed input array that comes from post. Uncleansed, it looks like

header("location:$states[$_POST['state']]");

If you're just learning, your first stop should not be the shortest distance between task and completion, you need to **start** with cleansing that input. There are lots of posts here about this, but a couple important fundamentals:

"Our users won't be inputting bad data to our forms." It's not your regular users you have to worry about.

"Accept only what you want, and throw everything else away." Too often coders apply patch on patch or filter on filter to screen input, but the simplest thing to start with is this concept. For example, if you expect a *two character input for state,* error out if anything else is found.

Error trap, error trap error trap - but do it in a user friendly manner. A lot of programming is like this

if (bad data) {
echo "Nasty spammer!";
exit;
}

Leaving the innocent user to use their back button, and often on some servers dumping all their data in the form due to caching. So people go the long (wrong) way around the fence, setting session variables to maintain data.

If you just "clean" the input, and error only when you have to, then make it east for them to fix it, it makes for a good solid program that is usable. A framework could be like this.

1. cleanse input, throwing away what you don't want.
2. NOW check for required fields, that all data after cleansing is **exactly** what you want.
3. If all OK in #2, proceed, otherwise return to your form with the errors clearly prominent and all the input values maintained.

Consider all of the above before you begin cobbling together a script from free code on the Internet. Bad habits are far too easy to form with PHP because it's so easy to learn, and much of the code posted on the Internet is just . . . the wrong way to go about it. :-)

Terabytes

5:52 pm on Jul 26, 2010 (gmt 0)

10+ Year Member



ok... my mistake for some of the syntax... wasn't paying attention.

here's some revised code:

<html>
<form action='form_handler.php' method='POST'>
<input type='text' name='name'>
<input type='text' company='company'>
<input type='text' address='address'>
<input type='text' city='city'>
<select size="1" name="state">
<option selected>Choose Your state</option>
<option value="Alabama">Alabama</option>
<option value="Alaska">Alaska</option>
<option value="Arizona">Arizona</option>
<option value="Arkansas">Arkansas</option>
<option value="California">California</option>
<option value="Colorado">Colorado</option>
<option value="Connecticut">Connecticut</option>
<option value="Delaware">Delaware</option>
<option value="District of Columbia">District of Columbia</option>
<option value="Florida">Florida</option>
<option value="Georgia">Georgia</option>
<option value="Hawaii">Hawaii</option>
<option value="Idaho">Idaho</option>
<option value="Illinois">Illinois</option>
<option value="Indiana">Indiana</option>
<option value="Iowa">Iowa</option>
<option value="Kansas">Kansas</option>
<option value="Kentucky">Kentucky</option>
<option value="Louisiana">Louisiana</option>
<option value="Maine">Maine</option>
<option value="Maryland">Maryland</option>
<option value="Massachusetts">Massachusetts</option>
<option value="Michigan">Michigan</option>
<option value="Minnesota">Minnesota</option>
<option value="Mississippi">Mississippi</option>
<option value="Missouri">Missouri</option>
<option value="Montana">Montana</option>
<option value="Nebraska">Nebraska</option>
<option value="Nevada">Nevada</option>
<option value="New Hampshire">New Hampshire</option>
<option value="New Jersey">New Jersey</option>
<option value="New Mexico">New Mexico</option>
<option value="New York">New York</option>
<option value="North Carolina">North Carolina</option>
<option value="North Dakota">North Dakota</option>
<option value="Ohio">Ohio</option>
<option value="Oklahoma">Oklahoma</option>
<option value="Oregon">Oregon</option>
<option value="Pennsylvania">Pennsylvania</option>
<option value="Rhode Island">Rhode Island</option>
<option value="South Carolina">South Carolina</option>
<option value="South Dakota">South Dakota</option>
<option value="Tennessee">Tennessee</option>
<option value="Texas">Texas</option>
<option value="Utah">Utah</option>
<option value="Vermont">Vermont</option>
<option value="Virginia">Virginia</option>
<option value="Washington">Washington</option>
<option value="West Virginia">West Virginia</option>
<option value="Wisconsin">Wisconsin</option>
<option value="Wyoming">Wyoming</option>
<option value="Not In USA">Not In USA</option>
<input type='text' name='zip'>
<input type='submit' value='Click'>
</form>
</html>

<?php

$name = $_POST['name'];
$company = $_POST['company'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];

?>


I fully understand the need for cleaning the input, and I promise I'll work on that, I just need a working example in order to appease the upper management.

How to do redirect to "page.htm" depending upon my state selection?

Thanks!

rocknbil

9:37 pm on Jul 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I thought I showed you? :-) Only small changes using your example.

$states = Array(
// other states here
'Texas' => 'texas.html',
// more states
);

// your code ....
$state = $_POST['state'];

header("location:".$states[$state]);

(I had an error, needed concatenation like above.)

If you build that states array, and do it at the top of your program, look what you can do:


$stateList = '<select size="1" name="state">
<option selected>Choose Your state</option>';
foreach ($states as $st=>$url) {
$stateList .= '<option value="' . $st . '"';
if (isset($_POST['state']) and ($_POST['state']==$st)) {
$stateList .= ' selected';
}
$stateList .= '>' . $st . '</option>';
}
$stateList .= '</select>';

echo $stateList;