[英]Can not set value in drop down list dynamically using PHP
I need one help.i need to set drop down value which is fetching from DB using PHP.I am explaining my code below. 我需要一个帮助。我需要设置使用PHP从数据库中获取的下拉值。我在下面解释我的代码。
<?php
$getcustomerobj = $dbobj->getFeedbackData($db,$id);
echo ($getcustomerobj->companypro);
?>
<select class="form-control" id="company_pro" name="company_pro" onChange="selectProductCompany(this.value)">
<option value="">Select Company/Product</option>
<option value="1" <?php if($getcustomerobj->companypro == 1 and $_REQUEST['companypro'] == 1){ print 'selected'; }?>>Select Company</option>
<option value="0" <?php if($getcustomerobj->companypro == 0 and $_REQUEST['companypro'] == 0){ print 'selected'; }?>>Select Product</option>
</select>
Here i can get the value of $getcustomerobj->companypro
as 1 but still unable to set it inside the drop down list.Please help me. 在这里我可以将
$getcustomerobj->companypro
的值$getcustomerobj->companypro
为1,但仍无法在下拉列表中进行设置。请帮助我。
You can add a default value for $_REQUEST['companypro']
at the top of your code: 您可以在代码顶部为
$_REQUEST['companypro']
添加默认值:
if (!isset($_REQUEST['companypro']))
$_REQUEST['companypro'] = 0;
In this case, you have already $_REQUEST['companypro']
variable, because at the first load it is empty. 在这种情况下,您已经有
$_REQUEST['companypro']
变量,因为在第一次加载时该变量为空。
This might work 这可能有效
<select class="form-control" id="company_pro" name="company_pro" onChange="selectProductCompany(this.value)" autocomplete="off">
<option value="">Select Company/Product</option>
<option value="1" <?php if($getcustomerobj->companypro == 1 and $_REQUEST['companypro'] == 1){ print 'selected="selected" '; }?>>Select Company</option>
<option value="0" <?php if($getcustomerobj->companypro == 0 and $_REQUEST['companypro'] == 0){ print 'selected="selected" '; }?>>Select Product</option>
</select>
If it doesnt work then add to the form element add autocomplete="off". 如果不起作用,则向表单元素添加autocomplete =“ off”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.