简体   繁体   中英

Can't show the query result in select option

UPDATE

I'm working with codeigniter, and i have simple select option like 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); 
  ?> 
    <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 :

 <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:

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?

在此输入图像描述

Can someone help me pls?

the problem lies in using a variable $idbrang in the manual written where clause. 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

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. I think you'll need to change this to use a PHP value for the assignment.

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. $jj will returns exactly what you put into quotes.

One way to do what you expect is using Ajax to pass javascript values to a PHP file using POST/GET methods. Here are more details .

This : id="field_' + count + '" name="fields[]' + '" will not work too because you're using a javascript variable into HTML. You can set your select id using plain javascript into a <script> tag.

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