根据下拉框选择生成的值填充文本字段

时间:2017-10-10 01:00:11

标签: php mysqli

我试图根据所选员工的价值取得相关的津贴价格。当我选择一名员工时,会生成津贴类型,但每种津贴类型都会回显相同的价格。

allowance price is same in all text fields.

我尝试了以下代码,但无法找到错误。你能帮我找一下代码中的错误吗?

            <div class="form-group">
               <label class="control-label col-lg-4" for="advanceAmount">Allowances: </label>
              <div class="col-lg-3"> 

                  <?php 
                            for($i = 0; $i < count($b); $i++){
                                echo '<input type="text" class="form-control input-sm margin-bottom" name="allowance_type" value="' . $b[$i] . '">';
                            }
                            ?>

                        </div>
                        <div class="col-lg-3"> 

                            <?php 
                              $i = 0;                               
                              $s = mysqli_query($connection,"select * from allowances_payroll where allowance_name = '$b[$i]'");

                              $r = mysqli_fetch_assoc($s);

                                    for($i = 0; $i < count($b); $i++){
                                     echo '<input type="text" class="form-control input-sm margin-bottom"  value="' . $r['allowance_amount'] . '">';  
                            }
                            ?>

                        </div>
                    </div>

$ b是来自数据库的爆炸津贴类型。

     if($result1){
        while($record = mysqli_fetch_array($result1)){
            $empId = $record['emp_id'];
            $department = $record['dep_name'];
            $salary = $record['emp_basicSalary'];
            $designation = $record['emp_classification'];

            $allowance = $record['allowances_types'];
            $b = explode(" ,", $allowance);                 

        }
      }

由于

1 个答案:

答案 0 :(得分:0)

你在错误的地方循环。 将您的代码更改为:

for($i = 0; $i < count($b); $i++){                             
  $s = mysqli_query($connection,"select * from allowances_payroll where allowance_name = '{$b[$i]}'");

  $r = mysqli_fetch_assoc($s);

    echo '<input type="text" class="form-control input-sm margin-bottom"  value="' . $r['allowance_amount'] . '">';  
  }
}

还要注意$b[$i]周围的{},或者你可以像其他两个一样连接。