你确定这个功能是你需要的吗?您想更改表单的操作而不是在一个地方处理重定向吗?
让我们从这里开始:您的表单正在使用 post,但您查找的是 $_GET['zip']。你需要改变它来听一个或另一个。由于这看起来像一个过滤形式,我建议使用 get:
<form method='get' id='gform_1' action='front-page.php'>
你不需要 enctype ,你的方法应该是 get 。现在,您的提交代码应该有 isset 围绕它:
if(isset($_GET['zip'])){
$sandiego = array('91911', '91914', '91915', '91932', '91942', '91945', '91950', '92014', '92025', '92027', '92029', '92037', '92064', '92065', '92067', '92071', '92075', '92101', '92102', '92103', '92104', '92105', '92106', '92107', '92108', '92109', '92110', '92111', '92113', '92114', '92115', '92116', '92117', '92118', '92119', '92120', '92121', '92122', '92123', '92124', '92126', '92127', '92128', '92129', '92130', '92131', '92132', '92134', '92135', '92139', '92140', '92145', '92147', '92154', '92173');
if (in_array($_GET['zip'], $sandiego)){
header("Location: http://www.pureflo.com/"); // <- you sure about this?
} else {
header("Location: http://www.pureflo.com/");
}
}
您的邮编检查器针对两者定位相同的位置。也许您应该使用当前没有名称的选择,因此它不会填充到 get 数组中:
if (in_array($_GET['zip'], $sandiego)){
header("Location: ".urldecode($_GET['selection'])."?zip=".$_GET['zip']."&type=".$_GET['type']);
} else {
header("Location: http://www.pureflo.com/");
}
exit;
并确保为您的选择输入 html 命名:
<select name="selection">
除非您真的想在几个不同的地方编写提交处理代码或包含它,否则您不需要那个 js。PS - 你不想那样做。如果你这样做,你会感到困惑。
您的单选按钮需要值,并且可能应该有一个不太具体的名称:
<input name="type" type="radio" id="homeradio" value="home" />
<input name="type" type="radio" id="officeradio" value="office" />
您可能希望根据此表格的实际意图更新您的问题。我假设它正在请求服务信息。就像 - 你说你想要家里的水,它应该带你去:
http://50.22.79.62/~pftech/water-delivery-service/?zip=xxxxx&type=home
这很简单。
您的整个代码将是这样的:
<?php
if(isset($_GET['zip'])){
$sandiego = array('91911', '91914', '91915', '91932', '91942', '91945', '91950', '92014', '92025', '92027', '92029', '92037', '92064', '92065', '92067', '92071', '92075', '92101', '92102', '92103', '92104', '92105', '92106', '92107', '92108', '92109', '92110', '92111', '92113', '92114', '92115', '92116', '92117', '92118', '92119', '92120', '92121', '92122', '92123', '92124', '92126', '92127', '92128', '92129', '92130', '92131', '92132', '92134', '92135', '92139', '92140', '92145', '92147', '92154', '92173');
if (in_array($_GET['zip'], $sandiego)){
header("Location: ".urldecode($_GET['selection'])."?zip=".$_GET['zip']."&type=".$_GET['type']);
} else {
header("Location: http://www.pureflo.com/");
}
exit;
}
?>
<form method='get' id='gform_1' action='front-page.php'>
<div class="serviceinput">
<label for="services">Services: </label>
<select id="selection" name="selection">
<option value=''>Select a Service</option>
<option value='http://50.22.79.62/~pftech/water-delivery-service/'>Water Delivery</option>
<option value='http://50.22.79.62/~pftech/coffee-delivery/'>Coffee Services</option>
<option value='http://50.22.79.62/~pftech/water-filtration-systems/'>Water Filtration</option>
</select>
</div>
<div class="zipcode">
<label for="zip">Zip Code: </label>
<input name="zip" type="text" maxlength="5" id="zip" />
</div>
<div class="frontradio">
<input name="type" type="radio" id="homeradio" value="home" />
<div class="homelabel"> <label for="homeradio">Home</label></div>
<input name="type" type="radio" id="officeradio" value="office" />
<label for="officeradio">Office</label>
</div>
<div class="homebutton">
<input type='submit' id="submithome" name="did_submit" value="Get Started!">
</div>
</form>
结束语,您的代码应该验证是否为服务下拉列表选择了一个值,并且只返回错误而不是强迫用户。
PS - 您的提交处理需要在任何 html 发送到浏览器之前完成。我的示例没有正确显示分离。它看起来是内联的,但这不是意图。表单检查代码应尽可能靠近文件顶部
JAVASCRIPT 示例:
<script type="text/javascript">
var sandiego = ['91911', '91914', '91915', '91932', '91942', '91945', '91950', '92014', '92025', '92027', '92029', '92037', '92064', '92065', '92067', '92071', '92075', '92101', '92102', '92103', '92104', '92105', '92106', '92107', '92108', '92109', '92110', '92111', '92113', '92114', '92115', '92116', '92117', '92118', '92119', '92120', '92121', '92122', '92123', '92124', '92126', '92127', '92128', '92129', '92130', '92131', '92132', '92134', '92135', '92139', '92140', '92145', '92147', '92154', '92173'];
var selection, zip, home;
function sendForm(){
zip = document.getElementById("selection").value;
if(inArray(zip, sandiego))
{
selection = document.getElementById("selection").value;
if(selection != "")
{
home = document.getElementById("home").value;
var url = selection + '?zip=' + zip + '&home=' + home;
window.location = url;
}
}
else
{
window.location = 'http://www.pureflo.com/';
}
}
function inArray(needle, haystack) {
var length = haystack.length;
for(var i = 0; i < length; i++) {
if(haystack[i] == needle) return true;
}
return false;
}
</script>