简体   繁体   中英

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
  $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.

You can add a default value for $_REQUEST['companypro'] at the top of your code:

if (!isset($_REQUEST['companypro']))
  $_REQUEST['companypro'] = 0;

In this case, you have already $_REQUEST['companypro'] variable, because at the first load it is empty.

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM