简体   繁体   English

如何根据选择的下拉菜单将用户发送到不同的结果页面?

[英]How do I send a user to a different results page depending on the dropdown selected?

I have a form that states what the specifications of a computer are which outputs a page with a title and description of the product when the submit button is clicked.我有一个表格,说明计算机的规格是什么,当点击提交按钮时,它会输出一个带有产品标题和描述的页面。 I want to add other devices to the form but I want the submit button to take me to a different results page depending on the type of product that has been selected in the form.我想向表单添加其他设备,但我希望提交按钮根据表单中选择的产品类型将我带到不同的结果页面。

I think the best/easiest way to do this and maintain it, is to add a score to the type of product and the submit button will take you to a certain page depending on the score.我认为最好/最简单的方法来做到这一点并维护它,是为产品类型添加一个分数,提交按钮将根据分数将您带到某个页面。

I've tried using if statements that apply a score to the variable:我尝试使用 if 语句将分数应用于变量:

if (type == "Desktop") {
    var = 1;
}
if (type == "Laptop") {
    var = 1;
}
if (type == "All-In-One") {
    var = 1;
}
if (type == "Projector") {
    var = 2;
}

This broke the page by stopping the code below from generating the desired content.这通过阻止下面的代码生成所需的内容来破坏页面。 This is the code the variable will be generating from, including where the submit button takes us and the button itself:这是变量将从中生成的代码,包括提交按钮带我们的位置和按钮本身:

<form method="post" attribute="post" action="pos.php">   

<label>Item Type</label>
<select name="type" id="type" onchange="test()">
    <option value=""></option>
    <option value="Laptop">Laptop</option>
    <option value="Desktop">Desktop</option>
    <option value="All-In-One">All-In-One</option>
    <option value="Projector">Projector</option>
</select><br>
...
<button type="submit" name="answer" id="answer" value="answer"><strong>Create</strong></button>

I want to be able to go to pos.php for computer based products, to proj.php for projectors and so on, we're expecting a large variety of products coming in and I don't want to bog down pos.php with a load of code that only I can distinguish and replicate.我希望能够访问基于计算机的产品的 pos.php,投影仪的 proj.php 等等,我们期待有各种各样的产品进入,我不想让 pos.php 陷入困境一堆只有我才能区分和复制的代码。

If I understood correctly then you could structure an array in PHP like below and assign the key as the url that will become the redirection endpoint when the form is submitted.如果我理解正确,那么您可以在 PHP 中构造一个数组,如下所示,并将键分配为 url,当表单提交时,它将成为重定向端点。 The javascript code that modifies the visibility of the DOM elements is triggered by changing the selected option in the SELECT element.修改 DOM 元素可见性的 javascript 代码是通过更改 SELECT 元素中的选定选项来触发的。 The below has not been tested or debugged for syntax/typos but might give an idea how to do it.以下内容尚未针对语法/错别字进行测试或调试,但可能会给出如何去做的想法。

<select name='type'>
    <?php

        $types=array(
            'pos.php'   =>  array(
                'Desktop',
                'Laptop',
                'All-In-One',
                'Server'
            ),
            'proj.php'  =>  array(
                'Projector',
                'TV'
            )
        );

        foreach( $types as $type => $arr ){
            foreach( $arr as $item ){
                printf( "<option value='%s'>%s", $type, $item );
            }
        }
    ?>
</select>




<script>

    const block=['grade','screensize','formfactor','brand','model','cpu','ghz','ram','hdd','storage_type','optical','os','issues'];
    const none={
        'desktop':['screensize'],
        'laptop':['formfactor'],
        'all-in-one':['formfactor'],
        'switch':['screensize','formfactor','cpu','ghz','ram','hdd','storage_type','optical','os'],
        'server':['screensize','formfactor','storage_type','os'],
        'projector':['screensize','formfactor','cpu','ghz','ram','hdd','storage_type','optical','os']
    };

    document.querySelector( 'select[name="type"]' ).onchange=function(e){
        block.forEach( item=>{
            let description=this.options[ this.options.selectedIndex ].text.toLowerCase();
            document.getElementById( item ).style.display='block';
            if( none.hasOwnProperty( description ) ){
                none[ description ].forEach( item =>{
                    document.getElementById( item ).style.display='none';
                })
            }
        })
    };
</script>

暂无
暂无

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

相关问题 如何根据在“下拉列表 A”上选择的选项禁用“下拉列表 B”的某些选项? - How do I disable certain options of 'Dropdown B' depending on an option selected on 'Dropdown A'? 如何确定是否选中了复选框下拉菜单的不同部分? - How do I determine if different sections of a checkbox dropdown menu are selected? 如何根据用户当前所在的页面呈现相同的组件? - How do I render same component depending on the page the user is currently on? 一个按钮如何根据选择的单选按钮显示不同的结果 - How can a button display different results depending on radio button selected 如何设置一个页面,根据导航中选择的链接显示不同的内容? - How can I set up a single page that I display different contents depending on what link is selected in the navigation? 如何根据下拉列表中的选定选项显示元素 - How can I show an element depending on selected option in a dropdown 如何在选择相同选项的情况下重定向到其他页面? - How do I redirect to different page with same option selected? 如何根据从“角度”下拉列表中选择的值路由到其他位置? - How do I route to a different location based on the values selected from a dropdown in angular? 如何根据文本框中的用户输入更改下拉列表中的选定项目? - How do I change the selected item in a dropdown based on user input in a text box? 如何使用JavaScript在JSP页面上的下拉列表中呈现选定值的属性? - How do I render attributes for a selected value from a dropdown, on a JSP page, using JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM