[英]HTML Default Option in Select Using PHP Is Not Working
I'm bug-proofing a form that allows data editing for book entries in a database. 我正在验证一种表单,该表单允许对数据库中的图书条目进行数据编辑。 Everything is working except for the drop-down box. 除下拉框外,其他所有功能均正常。 The drop-down box automatically populates itself with every unique entry in a specific field in the database table, and that part works perfectly. 下拉框自动使用数据库表中特定字段中的每个唯一条目填充自身,并且该部分可以完美运行。 However, when people click to edit a book all the fields are populated with that books information, and I wanted the drop-down box to default to the correct value for that book. 但是,当人们单击以编辑一本书时,所有字段都填充有该书的信息,我希望下拉框默认为该书的正确值。 My solution was to check each value as it populates the drop-down box against the actual book's value for that field and if they match, make it the "selected" value. 我的解决方案是检查每个值,因为它将针对该字段的实际书籍的值填充下拉框,如果它们匹配,则将其设为“选定”值。
It is not working. 它不起作用。 The box is still populating fine, but it is not defaulting. 该框仍然可以正常运行,但不是默认设置。 Here is the code for the drop-down box. 这是下拉框的代码。
<span style="margin-left:10px;">
Publication Type:
<select name="publicationType" >
<option value=""></option>
<option value="">-------------------------</option>
<?php
$lPub = '';
if(array_key_exists('publicationType',$_REQUEST)) $lPub = $_REQUEST['publicationType'];
$lPubArr = $datasetManager->getPublicationType();
foreach($lPubArr as $pubStr){
if($pubStr == $bookArr['publicationType']){
echo '<option '.($lPub==$pubStr?'selected="selected"':'').'>'.$pubStr.'</option>'."\n";
}
else{
echo '<option '.($lPub==$pubStr?'':'').'>'.$pubStr.'</option>'."\n";
}
}
?>
</select>
</span>
I can provide what all the variables are if needed. 如果需要,我可以提供所有变量。 I don't see what I'm doing wrong, but maybe someone will be able to catch an obvious mistake. 我看不到我在做什么错,但是也许有人可以发现一个明显的错误。
Thank you, Kai 谢谢你,凯
Not sure this will help but try this: 不确定这是否有帮助,但是请尝试以下操作:
<?php
$lPub = '';
if( array_key_exists('publicationType',$_REQUEST) )
$lPub = $_REQUEST['publicationType'];
$lPubArr = $datasetManager->getPublicationType();
foreach($lPubArr as $pubStr){
echo '<option '.($lPub==$pubStr?'selected="selected"':'').'>'.$pubStr.'</option>'."\n";
}
I removed this condition: 我删除了这种情况:
f($pubStr == $bookArr['publicationType'])
since I didn't get what the $bookArr['publicationType']
is used for, perhaps you left it there by mistake 因为我没有得到$bookArr['publicationType']
的用途,所以也许您把它留在了那里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.