通过多次选择从HTML中的JavaScript处理PHP中的用户数据



这只是一个公平的警告:我是一个HTML、PHP和JavaScript的新手。

我写了代码来获得多个选择框,其中第二个框中的可选择内容取决于选择第一个框。

就像:

Brands:      if Asus is selected the next box:                     and the last box is prime is selected:
Asus         Prime                                                 B560
MSI          Plus                                                  Z590 etc.

这对于新型号、品牌等来说必须是可消耗的。

到目前为止,我拥有的准则:

index.html

<!DOCTYPE html>
<head></head>
<body>

<form name="form1" id="form1" action="redir.php">

Hersteller: <select name="hersteller" id="hersteller">
<option value="" selected="selected">-wählen-</option>
</select>
<br><br>
Modell: <select name="model" id="model">
<option value="" selected="selected">-wählen-</option>
</select>
<br><br>
Chipset: <select name="chipid" id="chipid">
<option value="" selected="selected">-wählen-</option>
</select>
<br><br>
<input type="submit" value="weiter">
<script>var subjectObject = {
"Asus": {
"Prime": ["B560", "Z490", "Z590", "Z690"],
"ProArt": ["Z490"],
"ROG": ["Z690 ITX"]
},
"MSI": {
"Torpedo": ["Z490", "Z590", "Z690"]
}
}
window.onload = function() {
var subjectSel = document.getElementById("hersteller");
var topicSel = document.getElementById("model");
var chapterSel = document.getElementById("chipid");
for (var x in subjectObject) {
subjectSel.options[subjectSel.options.length] = new Option(x, x);
}
subjectSel.onchange = function() {
chapterSel.length = 1;
topicSel.length = 1;
for (var y in subjectObject[this.value]) {
topicSel.options[topicSel.options.length] = new Option(y, y);
}
}
topicSel.onchange = function() {
chapterSel.length = 1;
var z = subjectObject[subjectSel.value][this.value];
for (var i = 0; i < z.length; i++) {
chapterSel.options[chapterSel.options.length] = new Option(z[i], z[i]);
}
}
}</script>

</form> 
</body>

redir.php


<?php

$hersteller = $_POST["hersteller"];
$chipid = $_POST["chipid"];
$model = $_POST["model"];
if ($hersteller = "Asus") {
if($model = "Prime"){
if($chipid = "B560"){
header("Location: /driver_sites/Asus_Prime_B560.html"); 
}
}
exit();
}
//elseif ($items = 'Item2') {
//header("Location: item2.php");
//}
?>

我想处理选定的数据并重定向到特定的网站。例如如果用户选择";华硕"->quot;Prime"->quot;B560〃;并点击";weiter";(提交(,它应该将他重定向到像Asus_Pime_B560.html 这样的页面

如何做到这一点?提前感谢:(

我在PHP中尝试了多种方法,但似乎都不起作用,而且我的知识有限。此外,我搜索了整个互联网,但没有结果。

我希望我理解你的问题,改变你的"redir.php";文件如下:

<?php
$hersteller = $_POST["hersteller"];
$chipid = $_POST["chipid"];
$model = $_POST["model"];
$page = $hersteller.'_'.$model.'_'.$chipid.'.html';
header("Location: /driver_sites/".$page);

这样做的目的是建立";页面文件名";基于输入并重定向到它。假设用户选择了以下值:

$hersteller = 'Asus';
$model = 'Prime';
$chipid = 'B560';
$page = $hersteller.'_'.$model.'_'.$chipid.'.html';

$page现在=>Asus_Pime_B560.html

此示例意味着所有页面都具有相同的格式HERSTELLER_MODEL_CHIPID.html

编辑:根据你的评论,我会添加一些信息

当你看到一些URL包含?some=thing&foo=在?(问号(称为查询字符串。PHP可以使用$_GET superglobal访问此查询字符串参数(即读取参数foo i can do

$foo = $_GET['foo']; //$foo now contains "bar"

另一方面,如果表单是使用POST方法提交的(<表单方法="POST"……>(,则可以使用$_POST超全局访问数据

$something = $_POST['paramNam'];

最新更新