当用户选择某个选项时,-6ren">
gpt4 book ai didi

PHP 和 mysql : how can i make selected option stay selected in s Combo after submit

转载 作者:行者123 更新时间:2023-11-29 00:49:01 25 4
gpt4 key购买 nike

我在 stackoverflow 中搜索,我唯一得到的是类似 this 的东西.我想用这段代码来做

<select class="input_select" name='nombre_compania'><?
msqlcon_catering(); //which is the function i made that connects to the database
$query = "SELECT * FROM compania ORDER BY id DESC";
$result = mysql_query($query);
while ($r = mysql_fetch_array($result))
{
echo "<option value=\"". $r['id'] ."\">". $r['compania_nombre'] ."</option>";
}?>
</select>

当用户选择某个选项时,如果他遗漏了某些内容或输入错误,则在提交表单后保持选中状态。谢谢

最佳答案

<?php
$selected = null;
if(isset($_POST['nombre_compania']))
{
$selected = $_POST['nombre_compania'];
}

?>

<select class="input_select" name='nombre_compania'><?
msqlcon_catering(); //which is the function i made that connects to the database
$query = "SELECT * FROM compania ORDER BY id DESC";
$result = mysql_query($query);
while ($r = mysql_fetch_array($result))
{
echo "<option value=\"". $r['id'] ."\" ".($selected == $r['id'] ? 'selected=\"selected\"': '').">". $r['compania_nombre'] ."</option>";
}?>
</select>

关于PHP 和 mysql : how can i make selected option stay selected in s Combo after submit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9286962/

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