简体   繁体   English

使用PHP通过表单重定向多个URL

[英]Multiple URL redirects through a form using PHP

I was hoping to see if I could get some help from yeah on a project I am working on. 我希望看到我能否在我正在进行的项目上得到一些帮助。

I am super lost right now, but what I need to happen is when the user fills in the form if they choose one of the drop-downs and the home radio button is selected as well as them entering a San Diego zip code it takes them to whatever that url is within that drop-down. 我现在很迷茫,但我需要发生的是当用户填写表单时,如果他们选择了一个下拉菜单并且选择了主页单选按钮,并且他们输入圣地亚哥邮政编码就需要他们在该下拉菜单中的任何网址。

If they don't enter a San Diego zip code it takes them to a national page based on the drop-down. 如果他们没有输入圣地亚哥邮政编码,则会根据下拉列表将他们带到国家页面。 That part is actually fully complete and working. 那部分实际上是完全完整和有效的。 The client than turned around and said if they choose the office radio button and one of the selected drop-downs in the list it takes them to an office version page of whatever that drop-down was. 客户端转过身来说,如果他们选择办公室单选按钮和列表中选定的下拉菜单之一,则会将他们带到办公室版本页面,无论该下拉列表是什么。 Either being water delivery, coffee services or water filtration. 无论是送水,咖啡服务还是水过滤。

I am totally lost on how I could go about making the url redirect based on the different radio buttons, so they need the home radio button to go to the regular drop-downs and the office when selected to go to a completely different url based on the office option. 我完全迷失了如何根据不同的单选按钮进行网址重定向,因此他们需要主页单选按钮进入常规下拉菜单和办公室时选择进入完全不同的网址基于办公室选项。 So it would be office water delivery or office coffee services. 因此,这将是办公室供水或办公室咖啡服务。

I am super unsure of how to go about making three different url redirections based on one office radio button selection. 我非常不确定如何根据一个办公室单选按钮选择进行三种不同的URL重定向。 Anyways I really could use a hand on this thank you. 无论如何,我真的可以用手来谢谢你了。 The source code is below. 源代码如下。 Thanks 谢谢

<script type="text/javascript">
    function setAction(nPage){
        document.forms[0].action = nPage;
    }
</script>

<div class="home-form">
    <form method='get' id='gform_1' action='http://50.22.79.62/~pftech/form-handler/'>
        <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="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>
        </div>
    </form>
</div>

FORM HANDLER: FORM HANDLER:

<?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', '92562', '92563', '92590', '92591', '92592', '92596');
        if (in_array($_GET['zip'], $sandiego)){
            header("Location: ".$_GET['selection']."?zip=".$_GET['zip']."&type=".$_GET['type']);
        } else {
            header("Location: http://www.pureflo.com/");
        }
    }
    exit;
?>

SITE WHERE FORM IS ACTUALLY AT: 表格实际上是:

http://50.22.79.62/~pftech/

I think you're mixing up your priorities. 我认为你正在混淆你的优先事项。 For one, don't put the target id into those option values. 例如,不要将目标ID放入这些选项值。 Use plain values like '1', '2', '3' etc. 使用普通值,如“1”,“2”,“3”等。

The 'magic' happens in your evaluation method. “魔法”发生在您的评估方法中。 First, evaluate whether home or office is selected. 首先,评估是否选择了家庭或办公室。

if($_GET["home"] == 1) { //Assumes that selecting 'office' returns you this value.
} else {
}

Next, inside that selection, fill arrays with the target data. 接下来,在该选择内部,使用目标数据填充数组。

$urls = array();
if($_GET["home"] == 1) { //Assumes that selecting 'office' returns you this value.
    $urls[] = "http://50.22.79.62/~pftech/water-delivery-service/"
    //rinse and repeat
} else {
    $urls[] = "http://45.22.79.62/~pftech/water-delivery-service/"
    //rinse and repeat
}

At this point, you have a valid array, which relates directly to the selected dropdownbox in dependance of the selected radiobutton. 此时,您有一个有效的数组,它与选定的下拉框相关,与所选的单选按钮有关。

$URL = $urls[$_GET["selection"]];

Now you just need to pass that URL to your header function and you're good to go. 现在您只需要将该URL传递给标题函数,就可以了。

Just for the sake of completeness, here's the suggested code: 仅为了完整起见,这是建议的代码:

Handler: 处理器:

<?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', '92562', '92563', '92590', '92591', '92592', '92596');
        if (in_array($_GET['zip'], $sandiego)){
            $urls = array();
            if($_GET["home"] == 1) { //Assumes that selecting 'office' returns you this value.
                $urls[] = "http://50.22.79.62/~pftech/office_water-delivery-service/"
                $urls[] = "http://50.22.79.62/~pftech/office_coffee-delivery/"
                $urls[] = "http://50.22.79.62/~pftech/office_water-filtration-systems/"
            } else {
                $urls[] = "http://50.22.79.62/~pftech/home_water-delivery-service/"
                $urls[] = "http://50.22.79.62/~pftech/home_coffee-delivery/"
                $urls[] = "http://50.22.79.62/~pftech/home_water-filtration-systems/"
            }
            if($_GET['selection'] < 3 && $_GET['selection'] >= 0) {
                $URL = $urls[$_GET['selection']];
                header("Location: $URL?zip=$_GET[zip]");
            } else header("Location: http://www.pureflo.com/"); //Illegal input
        } else {
            header("Location: http://www.pureflo.com/");
        }
    }
    exit;
?>

Your HTML for the selection box would look like this: 您选择框的HTML将如下所示:

<select id="selection" name="selection">
    <option value='-1'>Select a Service</option>
    <option value="0">Water Delivery</option>
    <option value="1">Coffee Services</option>
    <option value="2">Water Filtration</option>
</select>

I hope this helps. 我希望这有帮助。

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

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