gpt4 book ai didi

php - 在 PHP 中隐藏/显示列表

转载 作者:太空宇宙 更新时间:2023-11-03 11:19:51 30 4
gpt4 key购买 nike

我有一个由 MySQL 生成的列表。当我单击列表中的一个项目时,它会在同一页面中显示与该项目相关的新列表。

编辑:

这是获取列表的代码。

<?php
include("./conn/connect.php");
$query = "SELECT * FROM entreprise ORDER BY id";
$result = mysql_query($query) or die("result failed: ".mysql_error());
while($row = mysql_fetch_assoc($result) ){
echo $row['name'].'<br>';
}
?>

我无法完成后半部分。

我怎样才能做到这一点?

最佳答案

您是否尝试在 PHP 中运行查询并在 HTML 页面的下拉列表中填充列表?如果是这样,您应该使用 JavaScript 根据在上一个列表中单击的内容来显示和隐藏它们。您可以使用事件来找出被点击的内容,提交表单或其他内容并获得相应的列表以显示在其下方。

我的问题解决了吗?还是我跑题了?

编辑:

好的。使用 JavaScript 隐藏显示元素不需要任何教程。

以表格形式列出列表。 onchange 事件将告诉您所选项目何时更改。当它发生时,将表格发布到同一页面。在 PHP 中,您可以获得所选项目。然后运行您的查询并根据此输入获取第二个列表。使用 Echo 或其他东西以 HTML 编写新列表。

<?php
// Here, $_POST['options'] will give you the selected item from first list.
// Run your query to get the next list and use echo() to write
// out the HTML or something
?>

<form name="myform" method="POST" action="test.php">
<select name="options" onchange="myform.submit();">
<option value="item1">item1</option>
<option value="item2">item2</option>
<option value="item3">item3</option>
</select>
</form>

<?php
// Use a 'for each' kind of loop here and run through the results
// of your second list. Construct the secondary select tag using
// the data here.
?>

您也可以在运行查询后使用 PHP 构建第一个列表。我没有这样做,因为代码会变得又长又复杂。

基本上您运行第一个查询并构建第一个列表并添加事件,如图所示。

当用户在该列表中选择某项时,您将其提交到同一页面,并根据该值运行第二个查询并构建二级列表。

关于php - 在 PHP 中隐藏/显示列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1865711/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com