gpt4 book ai didi

php - 单击过滤器按钮值重置后

转载 作者:行者123 更新时间:2023-11-29 21:16:30 24 4
gpt4 key购买 nike

我已经通过 PHP 通过 HTML 表从数据库创建了条目列表。有过滤器:下拉(选择选项)列表和文本过滤器。

PHP 看起来像:

<!DOCTYPE html>
<?php session_start();
ob_start();?>
<html>

<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
}
</style>
</head>

<body>
<form method="post" action="<?php $_PHP_SELF ?>">
<label for="rank">Rank applied</label>
<select id='english' name="RankApplied" class="test">
<option selected disabled value="0">Select value</option>
<option name="RankApplied" value="1">One</option>
<option name="RankApplied" value="2">Two</option>
<option name="RankApplied" value="3">Three</option>
</select>

<label for="VesselsType">Vessel's type</label>
<input id="VesselsType" type="text" name="VesselsType" class="etc" placeholder="Vessel's type"/>

<input type="submit" name="submit" value="Filter" />
</form>
<?php
require("connect.php");
include '../../wp-load.php';

$RankApplied = $_POST["RankApplied"];
$VesselsType = $_POST["VesselsType"];

$UserID = get_current_user_id();

if(isset($UserID)) {

$users = $con->prepare("
SELECT DISTINCT CONCAT(FirstName, ' ', LastName) AS FullName,
RankApplied,
VesselsType
FROM tbl
WHERE FirstName <> ''
AND (RankApplied = '$RankApplied' OR '$RankApplied' = 0)
AND (VesselsType = '$VesselsType' OR '$VesselsType' = '')
");
$users->execute();

$users->bind_result($FName, $RankApplied, $VesselsType);


} else {
echo "There is no User ID detected, try to refresh browser.";
}
?>

<table>
<tr>
<th>Name</th>
<th>Rank</th>
<th>Vessel Type</th>
<th>Preview</th>
</tr>
<?php
while ($users->fetch()) {
?>
<tr>
<td><?php echo $FName; ?></td>
<td><?php echo $RankApplied; ?></td>
<td><?php echo $VesselsType; ?></td>
<td>View</td>
</tr>
<?php
}
?>
</table>
</body>

</html>

如果我在单击 Filter 按钮后选择 RankApplied = 'Two'VesselsType = 'FooBar' 它会正确过滤结果,但是单击编辑按钮后,这些过滤器的值将重置(RankApplied = '选择值'VesselsType = ''),它应该保留/保存所选值。

最佳答案

代码不输出这些值。例如:

<input id="VesselsType" type="text" name="VesselsType" class="etc" placeholder="Vessel's type"/>

这只是输出一个空的输入。它没有包含已发布的值。您可以在这里获取值:

$VesselsType = $_POST["VesselsType"];

只需将其放在页面上方,以便您可以在输出中使用它。也许是这样的:

<input id="VesselsType" value="<?php=$VesselsType ?>" type="text" name="VesselsType" class="etc" placeholder="Vessel's type"/>

相同的概念适用于select,但稍微复杂一些。在这种情况下,您需要检查该值是否等于发布的值,并在 option 元素上设置 selected 属性。有很多种方法可以做到这一点,我的 PHP非常生锈,但是像这样的东西可能会成功:

<option <?php echo isset($_POST["RankApplied"]) ? "" : "selected"; ?> disabled value="0">Select value</option>
<option <?php echo $RankApplied == "1" ? "selected" : ""; ?> name="RankApplied" value="1">One</option>
etc...

无论你怎么做,要点都是一样的。您需要将这些值输出到页面,以便这些值出现在页面上。

关于php - 单击过滤器按钮值重置后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35897365/

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