dougan6402 2013-04-12 18:07
浏览 26
已采纳

使用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. 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. 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. 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 a a 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>
  • 写回答

2条回答 默认 最新

  • duanmajing9332 2013-04-12 18:35
    关注

    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']. You need to change that to listen for one or the other. Since this looks like a filtering form, I suggest using get:

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

    You don't need enctype there, and your method should be get. Now, your submission code should have isset around it:

    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. Perhaps you should utilize the select you made that currently has no name, so it won't populate in the get array:

        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:

    <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. PS - you don't want to do that. 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. 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:

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路