我正在尝试在我的网站上设置一个带有一个输入字段的表单。如果输入的邮政编码与邮政编码数组中的邮政编码相匹配,那么我希望将访问者带到特定的网址。如果输入与数组中的邮政编码不匹配,那么我希望访问者被带到不同的URL。
到目前为止,这是我的代码:
<form class="new_address" id="new_address" method="POST" action="<?php bloginfo('stylesheet_directory'); ?>/zipcodes.php">
<input placeholder="Enter your zip code here." type="text" id="zipcode" name="zipcode" />
</div>
<div class="small-4 columns">
<input class="button postfix" id="submit" type="submit" value="Book Now" />
</div>
</form>
这是zipcodes.php代码:
<?php
$allowedzips = array("33280", "33180 ", "33160 ", "33154 ", "33161", "33154", "33168", "33261", "33181", "33161", "33166", "33231", "33139", "33055", "33056", "33133", "33134", "33146", "33145", "33145", "33234", "33158", "33114", "33156", "33146", "33124", "33133", "33134", "33143", "33144", "33186", "33190", "33189", "33170", "33157", "33172", "33122", "33147", "33138", "33150", "33030", "33109", "33139", "33034", "33199", "33156", "33160", "33160", "33170", "33015", "33016", "33011", "33010", "33012", "33013", "33018", "33054", "33002", "33014", "33017", "33016", "33018", "33010", "33014", "33015", "33090", "33034", "33033", "33032", "33031", "33035", "33039", "33030", "33092", "33039", "33154", "33158", "33156", "33183", "33173", "33186", "33193", "33196", "33256", "33296", "33176", "33283", "33149", "33161", "33261", "33139", "33030", "33033", "33255", "33178", "33166", "33195", "33110", "33185", "33239", "33238", "33234", "33233", "33231", "33199", "33243", "33196", "33245", "33194", "33193", "33192", "33190", "33189", "33188", "33187", "33153", "33197", "33280", "33299", "33242", "33283", "33184", "33269", "33266", "33265", "33261", "33257", "33256", "33255", "33247", "33296", "33161", "33186", "33167", "33166", "33165", "33164", "33162", "33160", "33159", "33158", "33157", "33156", "33155", "33154", "33163", "33173", "33183", "33181", "33179", "33178", "33177", "33176", "33174", "33172", "33170", "33169", "33168", "33175", "33010", "33180", "33110", "33109", "33107", "33102", "33101", "33055", "33054", "33018", "33017", "33015", "33114", "33136", "33056", "33116", "33122", "33124", "33125", "33126", "33127", "33128", "33129", "33130", "33131", "33132", "33119", "33152", "33182", "33111", "33133", "33134", "33135", "33121", "33146", "33142", "33143", "33141", "33145", "33147", "33014", "33148", "33016", "33150", "33151", "33144", "33149", "33140", "33012", "33011", "33013", "33137", "33138", "33139", "33154", "33141", "33140", "33139", "33119", "33109", "33160", "33239", "33015", "33017", "33018", "33016", "33015", "33014", "33150", "33168", "33167", "33153", "33138", "33161", "33266", "33166", "33030", "33092", "33033", "33032", "33181", "33161", "33180", "33280", "33141", "33261", "33181", "33169", "33168", "33167", "33162", "33161", "33261", "33181", "33180", "33179", "33169", "33161", "33162", "33160", "33180", "33163", "33174", "33265", "33185", "33175", "33165", "33184", "33056", "33055", "33054", "33014", "33015", "33257", "33190", "33157", "33170", "33177", "33187", "33189", "33156", "33256", "33032", "33092", "33190", "33170", "33177", "33189", "33197", "33187", "33031", "33032", "33158", "33176", "33156", "33132", "33176", "33186", "33243", "33256", "33176", "33173", "33183", "33143", "33146", "33155", "33156", "33157", "33183", "33173", "33193", "33140", "33154", "33182", "33194", "33184", "33172", "33174", "33144", "33194", "33162", "33164", "33146", "33124", "33139", "33166", "33183", "33172", "33144", "33155", "33194", "33182", "33174", "33165", "33160");
$input = $_POST["zipcode"];
foreach($input as $zip) {
if (in_array($zip, $allowedzips)) {
header('Location: http://localhost:63951/booking');
exit;
}
else {
header('Location: http://localhost:63951/out-of-range');
exit;
}
}
?>
我哪里错了?我提交表单时得到的是zipcodes.php加载为白页。
任何帮助将不胜感激!
答案 0 :(得分:1)
没有必要迭代$input
- 实际上它没有任何意义。您需要做的就是查看$input
数组中是否$allowedzips
。我还使用trim()
修改了额外空格的用户输入,以防万一:
<?php
$allowedzips = array('..','...');
$input = trim($_POST["zipcode"]);
if (in_array($input, $allowedzips)) {
header('Location: http://localhost:63951/booking');
exit;
}
else {
header('Location: http://localhost:63951/out-of-range');
exit;
}
?>