2014-03-26 217 views
0

我有从表格填入数据库的数据。 第一个是组合框,第二个是文本框。使用php javascript填充组合框选择文本框。

我的问题是如何通过选择组合框来填充文本框。 我已经尝试使用JavaScript,但它没有奏效,请看看我的代码。

//这是HTML表单:当我选择的组合框nama_brg,它将搜索在tb_baranga价格,其中非农产品市场准入= nama_brg

echo '<tr>'; 
    echo '<td width=100px>Nama Barang</td>'; 
    echo '<td>:</td>'; 
    echo '<td>'; 
    echo '<select name="nama_brg" onchange="ambilharga()"> 
       <option value="kosong">PILIH</option>'; 

       $getbrg = "select * from tb_baranga"; 
       $query2 = mysql_query($getbrg); 
       while ($row2=mysql_fetch_array($query2)) 
       { 
        echo '<option value="'.$row2['nama'].'">'.$row2['nama'].'</option>'; 
       } 
      echo '</select>'; 

    echo '</td>'; 
    echo '</tr>'; 

//这是我的javascript

<script type="text/JavaScript"> 

     function ambilharga() 
     { 
     <?php 

     $cekbrg = "select * from tb_baranga"; 
     $hasil = mysql_query($cekbrg); 
     while ($row5 = mysql_fetch_array($hasil)) 
     { 
      $namabrg = $row5['nama']; 

      echo "if (document.add_invoice.nama_brg.value == '".$namabrg."')"; 
      echo "{"; 

      $gethrg = "select harga from tb_baranga where nama='".$namabrg."'"; 
      $hasil2 = mysql_query($gethrg); 
      while ($data2 = mysql_fetch_array($hasil2)) 
      { 
       $price = $data2['harga']; 
       echo "document.getElementById('harga2').innerHTML = '".$price."' "; 
       echo "document.getElementById('harga2').value = '".$price."' "; 
      } 


      echo "}"; 
     } 

     ?> 
     } 
    </script> 

这是我的表结构

tb_baranga 
nama varchar 30 
harga int 
+0

您需要在这里调用Ajax。当您选择组合值时,必须将Ajax请求发送到服务器并收集适当的文本。然后输出响应值 –

+0

你能举个例子吗? –

回答

0

试试这个:

请按照下面提到的步骤操作。

>> Check all values are coming proper in drop-down box. 
>> Call jquery ajax on change of dd box. (Ref. https://api.jquery.com/jQuery.ajax/) 
>> Show the value you get in ajax to text-box. 
+0

你能纠正我的代码吗? 我已经重新检查它,但它似乎很好 –

+0

试试你的自我我的朋友。您应该在上面定义的函数ambilharga()中进行更改。 –

+0

你能给我一些关于jQuery ajax的参考吗? btw thx为您的帮助兄弟。 –