gpt4 book ai didi

php - 以 PHP 生成的形式使用 MySQL 中的数据进行粘性选择

转载 作者:行者123 更新时间:2023-11-29 22:56:06 25 4
gpt4 key购买 nike

我有一个粘性的select表单,它从MySQL获取options的数据。如果未提交表单,则表单应显示“选择类型”(这有效)。然后我试着让它粘起来。我的想法是将 $_POST['type'] 与来自 MySQL 的数据进行比较,如果相同,则应回显所选内容。

当我尝试回显时,$_POST['type'] 工作正常,MySQL 数据库中的选项也正常工作。

我觉得我已经接近解决方案了,但我错过了一些东西。有什么想法吗?

<select type="text" name="type" id="type" class="form-control input-lg">
<option value="" disabled <?php if(!isset($_POST['submit'])){ echo "selected";} ?> >Select type</option>
<?php
$result = mysql_query("select * from type");

while ($row = mysql_fetch_assoc($result))
{
$type[] = $row;
}

$count = count($type);

for ($i = 0; $i < $count; $i++)
{
$selected = $_POST['type'];
echo "<option";

if($selected === $type[$i] ){
echo "selected";
}

echo ">";
echo $type[$i]['type'];
echo '</option>';
}
?>
</select>

最佳答案

更改行echo "selected";echo " selected ";

当前将回显 <optionselected>选定的选项格式错误。

关于php - 以 PHP 生成的形式使用 MySQL 中的数据进行粘性选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28714669/

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