如何在php中的下拉列表中设置默认值?
我的代码如下所示:如何在php中的下拉列表中设置默认值?,php,drop-down-menu,Php,Drop Down Menu,我的代码如下所示: <select autofocus="autofocus" name="SourceCountry" id="SourceCountry"> <?php populate_country();?> </select> 我把世界上所有的国家都纳入了这一下降趋势。我想要一个特定的国家名称(比如日本)作为默认选择。我不确定select标记的哪个属性或属性会执行此操作。我试着用了很多,但没有成功。有人能建议吗 国家的代码如下所示: i
<select autofocus="autofocus" name="SourceCountry" id="SourceCountry">
<?php populate_country();?>
</select>
我把世界上所有的国家都纳入了这一下降趋势。我想要一个特定的国家名称(比如日本)作为默认选择。我不确定select标记的哪个属性或属性会执行此操作。我试着用了很多,但没有成功。有人能建议吗
国家的代码如下所示:
if(connect_to_DB()==1)
$result=FetchCountriesList();
//mysqli_data_seek($result,0);
while($row = mysqli_fetch_assoc($result))
{
echo '<option value='.$row['CountryName'].'>'.$row['CountryName'].'</option>';
}
if(将_连接到_DB()==1)
$result=FetchCountriesList();
//mysqli_data_seek($result,0);
while($row=mysqli\u fetch\u assoc($result))
{
回显'.$row['CountryName'].';
}
在FetchCountriesList()中,有一个select查询,如:
从国家/地区中选择不同的国家/地区名称
谢谢只需在您希望选择的选项值上添加属性
selected=“selected”
例如:
<option value="Japan" selected="selected">Japan</option>
日本
要在函数中使用此选项,请将函数替换为:
function populate_country($selected = NULL) {
if(connect_to_DB()==1) $result=FetchCountriesList();
//mysqli_data_seek($result,0);
while($row = mysqli_fetch_assoc($result)) {
echo '<option value="'.$row['CountryName'].'" '.($selected == $row['CountryName'] ? 'selected="selected"' : NULL).'>'.$row['CountryName'].'</option>';
}
}
函数填充国家($selected=NULL){
如果(connect_to_DB()==1)$result=FetchCountriesList();
//mysqli_data_seek($result,0);
而($row=mysqli_fetch_assoc($result)){
回显'.$row['CountryName'].';
}
}
然后,您可以在HTML中使用此选项:
<select autofocus="autofocus" name="SourceCountry" id="SourceCountry">
<?php populate_country("Japan");?>
</select>
添加选择的
属性,例如:
<option value="test" selected="selected">Bla</option>
Bla
$defValue='Japan';
while($row=mysqli\u fetch\u assoc($result)){
回声';
echo$row['CountryName'].';
}
添加所选属性
。试试这个:
<select name="country">
<option value="kor">Korea</option>
<option value="rus">Russia</option>
<option selected="selected" value="jap">Japan</option>
</select>
韩国
俄罗斯
日本
在函数populate\u country()中为您的选项添加选定项。(在您只想选择为默认值的选项中)编辑您的函数并尝试查找日本并将此函数设置为选中:)
if(connect_to_DB()==1)$result=FetchCountriesList();
//mysqli_data_seek($result,0);
while($row=mysqli\u fetch\u assoc($result)){
如果($row['CountryName']!=“Japan”){
回显'.$row['CountryName'].';
}否则{
回显'.$row['CountryName'].';
}
}
您能给我们看一下populate_country()的代码吗代码>函数?只需将“selected”属性添加到包含日本的选项中…填充国家的代码如下:if(connect_to_DB()==1)$result=FetchCountriesList();//mysqli_data_seek($result,0);当FetchCountriesList()中的($row=mysqli_fetch_assoc($result)){echo'.$row['CountryName'].'.}时,有一个select查询,如:select distinct CountryName fromCountries@user2811464通过编辑将函数的代码添加到您的第一篇文章:)或者问题不清楚。或者这是一个关于HTML的简单问题。一个简单的谷歌搜索返回答案。将“seleteced”更改为“selected”,兄弟。我想他理解所选内容,但他不知道在哪里添加,因为内容来自php功能。嗨,康弗里克,我没有为所有单个值编写多条语句。我通过MYSQL获取国家名称,并在下拉列表中填充。我正在用PHP调用一个函数来填充国家名称。@EmilianManolache说实话,我两次丢失了全部答案!我认为他理解所选的东西,但他不知道在哪里添加它,因为内容来自php函数。@jorandenhuting问题的一部分:我不确定select标记的哪个属性或属性会这样做。所以我认为问题是关于属性:)我认为他理解所选的东西,但他不知道在哪里添加它,因为内容来自php功能。我认为问题很清楚:“我不确定select标记的哪个属性或属性会这样做。”嗨,Emilian,我无法添加所选属性。。。。这里我动态获取了许多国家的名称。。我只写了一个选项标签来获取所有的国家/地区名称,你能不能把selected not selected=“selected”??
<select name="country">
<option value="kor">Korea</option>
<option value="rus">Russia</option>
<option selected="selected" value="jap">Japan</option>
</select>
if(connect_to_DB()==1) $result=FetchCountriesList();
//mysqli_data_seek($result,0);
while($row = mysqli_fetch_assoc($result)) {
if($row['CountryName'] !== "Japan") {
echo '<option value='.$row['CountryName'].'>'.$row['CountryName'].'</option>';
} else {
echo '<option value='.$row['CountryName'].' selected="selected">'.$row['CountryName'].'</option>';
}
}
<select>
<option> Red </option>
<option selected="selected"> blue </option>
<option> yellow </option>
</select>
function populate_country($country){
$countries = array('algeria', 'japan', 'mexico', 'united kingdom' ..... );
foreach($countries as $country){
$selected = (strtolower($selected) == $country) ? ' selected="selected"' : null;
echo "<option$selected>".ucwords($country)."</option>\r\n";
}
}