[英]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.