URL Redirect from Zipcode Form Entry

2019-09-03 01:00发布

问题:

Okay. I've diligently attempted to figure this out on my own before asking for help, but 4 hours later and the first 4 pages of Google clicked, I'm at your mercy. I would like a simple one line form a user can enter their zip code and it will automatically redirect them to a partner site. Ie. User enters 80013 and it forwards them to www.domain1.com User enters 80303 and it forwards them to www.domain2.com

I know I'm missing something very small, but no matter what variation I try to use in the PHP, it will only go to domain1.com no matter what zip array I'm calling from.

html:

<form method='get' action='3.php'>
  <div class="zipcode">
    <label for="zip">Zip Code: </label>
    <input name="zip" type="text" maxlength="5" id="zip" />
  </div>

  <div class="button">
    <input type='submit' name="did_submit" value="Get Started!">
  </div>
</form>

3.php

<?php

$zip = $_POST['zip']; //your form method is post

$loc1 = array (80013,80015,80016,80017,80018,80019,80104,80107,80108,80109,80112,80116,80118,80120,80121,80122,80123,80124,80125,80126,80127,80128,80129,80130,80134,80135,80138,80160,80161,80162,80163,80165,80166,80433,80453,80465,80470);
$loc2 = array (80601,80602,80603,80614,80640,80022,80037,80241,80233,80229,80239,80240,80249,80238,80216,80202,80205,80206,80207,80209,80203,80218,80220,80222,80226,80231,80224,80246,80210,80230,80204,80237,80010,80012,80014,80040,80041,80042,80044,80045,80046,80047,80247,80110,80111,80150,80151,80113,80155,80102,80427,80621,80444,80137);
$loc3 = array (80303,80305,80302,80304,80301,80310,80466,80516,80026,80027,80020,80021,80023,80002,80003,80004,80005,80007,80030,80031,80401,80403,80427,80439,80452,80457,80422,80033,80234,80260,80221,80212,80211,80215,80214,80226,80228,80223,80204,80232,80227,80235,80236);

if(in_array($zip == $loc1)) {
    header("Location: http://www.domain.com");
} 

elseif(in_array($zip == $loc2)) {
    header("Location: http://www.domain2.com");
} 
else {
    header('Location: http://www.domain3.com');
}
exit();

?>

Please help!

回答1:

You almost had it. Try this: if(in_array($zip, $loc1))

When you type $zip == $loc1 it attempts to compare a string to an array, which will always return FALSE. Also, you need to set your form's method to GET or else assign $zip = $_GET['zip']; at the top of the script, to keep the request method consistent.

Lastly, this doesn't affect the outcome of your script, but both the exit(); and the closing tag (?>) are superfluous and have no effect here, presuming this is the entire script.



回答2:

You're using method='get' in your form, but in your PHP you're using $_POST. You need to pick one or the other.



回答3:

You are using the html form method="GET" and you are accessing the php file using $_POST. There is the problem.

The in_array() written by you is wrong

It should be for eg:

<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
    echo "Got Irix";
}
if (in_array("mac", $os)) {
    echo "Got mac";
}
?>


标签: php forms