简体   繁体   English

使用PHP的简单表单的多重定向

[英]Multi-redirect for a simple form using PHP

Please can anyone help me on this form, I need to have this done pretty much right now for my client and I am lost! 请任何人在此表格上为我提供帮助,我现在需要为我的客户做很多工作,我迷路了! Not the best at PHP. 不是最擅长PHP的。 What needs to happen is whenever a user fills out the form it needs to redirect the URL based on the inputs added for the form. 需要发生的是,每当用户填写表单时,它都需要根据为表单添加的输入来重定向URL。 For example if they choose any of the drop-downs listed it will take them to that specific page. 例如,如果他们选择列出的任何下拉列表,它将带他们到该特定页面。 I actually got all of that working through javascript as shown. 如图所示,我实际上通过javascript获得了所有这些功能。 The next part where I need help is when they enter a correct zip code being that of San Diego, it will take them to the page that they chose in the drop-down. 我需要帮助的下一部分是当他们输入正确的邮政编码时,即圣地亚哥的邮政编码,它将带他们到他们在下拉列表中选择的页面。 If they don't input a correct zip code of San Diego, it will take them to aa different 'nation wide page'. 如果他们没有输入正确的圣地亚哥邮政编码,则会将他们带到另一个“全国范围内的页面”。 Completely overriding the drop-down selections. 完全覆盖下拉选项。 I really need help with this, can anyone please help me out? 我真的需要帮助,有人可以帮我吗? Thanks for any input that can be added because I am super lost on what to do right now... 感谢您可以添加任何输入,因为我对现在的操作一无所知...

<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>

check the method of your from :) and after the header() use exit to prevent further execution of the php 从:)中检查您的方法,并在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; 
   }

?>

Are you sure this functionality is what you need? 您确定需要此功能吗? You want to change the action of the form instead of handling the redirecting in one spot? 您要更改表单的操作,而不是一处处理重定向?

Let's start here: Your form is using post, but you look for $_GET['zip']. 让我们从这里开始:您的表单正在使用post,但是您寻找的是$ _GET ['zip']。 You need to change that to listen for one or the other. 您需要更改它以侦听一个或另一个。 Since this looks like a filtering form, I suggest using get: 由于这看起来像是过滤形式,因此我建议使用get:

<form method='get' id='gform_1' action='front-page.php'>

You don't need enctype there, and your method should be get. 您不需要在那里进行enctype,并且您的方法应该是get。 Now, your submission code should have isset around it: 现在,您的提交代码应该包含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/");
    }
}

Your zip checker targets the same location for both. 您的zip检查器将两者定位到相同的位置。 Perhaps you should utilize the select you made that currently has no name, so it won't populate in the get array: 也许您应该利用当前没有名称的选择进行选择,因此它不会填充在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;

And make sure to give your selection input html a name: 并确保为您的选择输入html命名:

<select name="selection">

You do not need that js unless you really feel like writing submission handling code in several different places or including it. 除非您真的想在几个不同的地方编写或包含提交处理代码,否则不需要该js。 PS - you don't want to do that. PS-您不想这样做。 If you do, you're confused. 如果这样做,您会感到困惑。

Your radio buttons need values and should probably have a less specific name: 您的单选按钮需要值,并且应该使用不太明确的名称:

<input name="type" type="radio" id="homeradio" value="home" />
<input name="type" type="radio" id="officeradio" value="office" />

You may want to update your question with the actual intention of this form. 您可能需要使用此表单的实际意图来更新您的问题。 I would assume it is requesting service information. 我认为它正在请求服务信息。 Like - you say you want water for home and it should take you to something like: 像-您说要给家里的水喝,它应该带您去类似:

http://50.22.79.62/~pftech/water-delivery-service/?zip=xxxxx&type=home

This is pretty simple. 这很简单。

Your whole code would be something like this: 您的整个代码将如下所示:

<?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>

A closing note, your code should verify there is a value selected for the service dropdown and just return errors instead of forcing the user around. 结束语,您的代码应验证为服务下拉列表选择了一个值,并且仅返回错误而不是强迫用户走动。

PS - your submission handling needs to be done BEFORE any html is sent to the browser. PS-在将任何html发送到浏览器之前,您需要完成提交处理。 My example does not properly show a separation. 我的示例未正确显示分隔。 It looks inline but that is not the intention. 它看起来是内联的,但这不是意图。 The form check code should happen as close to the top of the file as possible 表单检查代码应尽可能靠近文件顶部

JAVASCRIPT EXAMPLE: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM