0

请任何人都可以帮助我填写此表格,我现在需要为我的客户完成这项工作,我迷路了!在 PHP 方面不是最好的。需要发生的是,每当用户填写表单时,它需要根据为表单添加的输入重定向 URL。例如,如果他们选择列出的任何下拉菜单,它会将他们带到该特定页面。如图所示,我实际上通过 javascript 完成了所有这些工作。我需要帮助的下一部分是当他们输入正确的邮政编码(圣地亚哥的邮政编码)时,它会将他们带到他们在下拉菜单中选择的页面。如果他们没有输入正确的圣地亚哥邮政编码,它将把他们带到一个不同的“国家页面”。完全覆盖下拉选择。我真的需要这方面的帮助,谁能帮帮我?感谢您提供任何可以添加的输入,因为我现在非常迷茫……

<script type="text/javascript">

    function setAction(nPage){

        document.forms[0].action = nPage;
    }
</script>

<?php
    $zip = $_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($zip, $sandiego)){
        header("Location: http://www.pureflo.com/");
    } else {
            header("Location: http://www.pureflo.com/");
    }

?>

<form method='post' enctype='multipart/form-data'  id='gform_1'  action='front-page.php'>
<div class="serviceinput"
<label for="services">Services: </label>
<select id="selection" onchange="setAction(this.value)">

<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>
&nbsp;
<div class="zipcode">
<label for="zip">Zip Code: </label>
<input name="zip" type="text" maxlength="5" id="zip" /></div>
<div class="frontradio"><input name="home" type="radio" id="homeradio" />
   <div class="homelabel"> <label for="homeradio">Home</label></div>
    <input name="home" type="radio" id="officeradio" />
    <label for="officeradio">Office</label></div>
<div class="homebutton">
<input type='submit' id="submithome" name="did_submit" value="Get Started!">
</div>
</form>
4

2 回答 2

1

检查你的方法:) 并在header()使用exit后防止进一步执行 php

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

    $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($zip, $sandiego)){
        header("Location: ".$_POST['selection']);
        exit;
    } else {
            header("Location: http://www.pureflo.com/");
            exit; 
   }

?>
于 2013-04-12T18:16:33.313 回答
0

你确定这个功能是你需要的吗?您想更改表单的操作而不是在一个地方处理重定向吗?

让我们从这里开始:您的表单正在使用 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>
  &nbsp;
  <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>
于 2013-04-12T18:35:59.137 回答