简体   繁体   English

无法在选择选项中显示查询结果

[英]Can't show the query result in select option

UPDATE UPDATE

I'm working with codeigniter, and i have simple select option like this: 我正在使用codeigniter,我有这样简单的选择选项:

<select id="field_' + count + '" name="fields[]' + '"  class="form-control no_inv" >
  <?php 
    $jj = "<script>var e = document.getElementById('id_barang').value;document.write(e);</script>"; 
    $noInv = $this->modelku->select_inv($jj); 
  ?> 
    <option value="" selected="selected" disabled>Pilih no inventaris</option> 
  <?php 
    foreach($noInv->result() as $inv){ ?> 
      <option value="<?php echo $inv->no_inv ?>">
        <?php echo $inv->no_inv ?>
      </option><?php } 
   ?>
</select><br>

And this is my html element with id = id_barang : 这是我的id = id_barang html元素:

 <select name="id_barang" id="id_barang" class="form-control">
    <?php $idBarang = $this->modelku->select_idBrang() ?>
    <?php foreach($idBarang->result() as $idBr){ ?>
        <option value="<?php echo $idBr->id_barang ?>"><?php echo $idBr->id_barang ?></option>
    <?php } ?>
</select required>

select_inv function from modelku: 来自modelku的select_inv函数:

public function select_inv($idbrang)
{
    $this->db->select("no_inv");
    $this->db->from('detail_barang');
    $this->db->where('kondisi', 'Ada');
    $this->db->where('id_barang ', $idbrang);
    $query = $this->db->get();
    return $query;
}

But when i click the select option, the value from no_inv doesn't appear in my select option? 但是,当我单击选择选项时,no_inv中的值不会出现在我的选择选项中?

在此输入图像描述

Can someone help me pls? 有人可以帮我吗?

the problem lies in using a variable $idbrang in the manual written where clause. 问题在于在手写的where子句中使用变量$idbrang To use a variable change the line 要使用变量更改该行

$this->db->where("kondisi = 'Ada' AND id_barang = '$idbrang' ");

to

$this->db->where('kondisi', 'Ada');
$this->db->where('id_barang ', $idbrang);

more info here 更多信息在这里

note: I gave this answer before the OP was completely remodeled 注意:在OP完全改造之前我给出了这个答案

You can't assign a value to a PHP variable by using javascript, since they have different times for execution, and when you use the script tag, unless the browser loads it, it will be only a PHP string. 您不能使用javascript为PHP变量赋值,因为它们具有不同的执行时间,并且当您使用脚本标记时,除非浏览器加载它,否则它将只是一个PHP字符串。 I think you'll need to change this to use a PHP value for the assignment. 我认为您需要更改此值以使用PHP值进行分配。

You can't do this: 你不能这样做:

<select id="field_' + count + '" name="fields[]' + '"  class="form-control no_inv" >
  <?php 
    $jj = "<script>var e = document.getElementById('id_barang').value;document.write(e);</script>"; 
    $noInv = $this->modelku->select_inv($jj); 
  ?>
<!-- // ... -->
</select>

Because the content of your PHP $jj variable is plain text, it will not select your #id_barang value. 因为PHP $jj变量的内容是纯文本,所以它不会选择#id_barang值。 $jj will returns exactly what you put into quotes. $jj将完全返回您放入引号的内容。

One way to do what you expect is using Ajax to pass javascript values to a PHP file using POST/GET methods. 实现预期目标的一种方法是使用Ajax使用POST / GET方法将javascript值传递给PHP文件 Here are more details . 这里有更多细节

This : id="field_' + count + '" name="fields[]' + '" will not work too because you're using a javascript variable into HTML. 这个: id="field_' + count + '" name="fields[]' + '"也不会起作用,因为你在javascript中使用了一个javascript变量。 You can set your select id using plain javascript into a <script> tag. 您可以使用纯JavaScript将您的选择ID设置为<script>标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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