php - Set value from database to textbox based on search by id -


please me solve case. have code below :

<script>     function getxmlhttp() {         var xmlhttp = false;             try{             xmlhttp=new xmlhttprequest();         }         catch(e)         {             try{                 xmlhttp= new activexobject("microsoft.xmlhttp");             catch(e){                 try{                     xmlhttp = new activexobject("msxml2.xmlhttp");                 catch(e1){                     xmlhttp=false;                 }             }         }         return xmlhttp;     }      function getcity(url, id1) {         var req = getxmlhttp(url);         if (req) {             req.onreadystatechange = function() {                 if (req.readystate == 4) {                     // if "ok"                     if (req.status == 200) {                         document.getelementbyid(id1).value=req.responsetext;                             // document.getelementbyid(id2).value=req.responsetext;                     } else {                         alert("there problem while using xmlhttp:\n" + req.statustext);                     }                 }             }             req.open("get", url, true);             req.send(null);         }     } </script>  <?php            echo "         <tbody>             <tr>                 <td class='td'><input type='text' size='6' name='kode_barang" ."' maxlength='6' onchange='javascript:this.value=this.value.touppercase();' onkeyup='getcity(\"search.php?kode_barang=\"+this.value, \"nama_barang"  . "\",\"harga_barang"  . "\");' id='kode_barang'></td>                 <td class='td'><input type='text' size='22' name='nama_barang"  . "' id='nama_barang" . "' readonly tabindex='1'></td>                 <td class='td'><input type='text' size='22' name='harga_barang"  . "' id='harga_barang" . "' tabindex='1'></td>         </tbody>     "; ?> 

search.php file :

include_once('config.php');  if (isset($_get['kode_barang'])) {     $qry = "select nama_barang, harga_barang t_barang kode_barang = '" . $_get['kode_barang'] . "'";     $sql = mysql_query($qry);      $ary = mysql_fetch_array($sql);      echo $ary['nama_barang'];     echo $ary['harga_barang']; } 

this how find nama_barang , harga_barang based on kode_barang input. example if input kode_barang in textbox kode_barang in text_box nama_barang , harga_barang set information database automatically.

i've tried in code, set 2 information(nama_barang & harga_barang) in 1 textbox nama_barang. should set in each textbox.

so, question : how set information nama_barang , harga_barang database each textbox ?

appreciate helps. thank you.

replace getcity() function following one

  function getcity(url, id1, id2) {             var req = getxmlhttp(url);             if (req) {                 req.onreadystatechange = function() {                     if (req.readystate == 4) {                         // if "ok"                         if (req.status == 200) {             var res = json.parse(req.responsetext);                             document.getelementbyid(id1).value=res.nama_barang;                               document.getelementbyid(id2).value=res.harga_barang;                         } else {                             alert("there problem while using xmlhttp:\n" + req.statustext);                         }                     }                 }              req.open("get", url, true);                 req.send(null);             }      } 

and replace part of code in search.php

from

$ary = mysql_fetch_array($sql);  echo $ary['nama_barang']; echo $ary['harga_barang']; 

to

$ary = mysql_fetch_array($sql);  echo json_encode($ary); 

the problem was, missing id of second text box in getcity() function.

update sending default value

replace following part

if (isset($_get['kode_barang'])) {     $qry = "select nama_barang, harga_barang t_barang kode_barang = '" . $_get['kode_barang'] . "'";     $sql = mysql_query($qry);      $ary = mysql_fetch_array($sql);      echo $ary['nama_barang'];     echo $ary['harga_barang']; } 

with

if (isset($_get['kode_barang'])) {     $qry = "select nama_barang, harga_barang t_barang kode_barang = '" . $_get['kode_barang'] . "'";     $sql = mysql_query($qry);      if (mysql_num_rows($sql) > 0)     {       $ary = mysql_fetch_array($sql);        echo json_encode($ary);     }     else     {        echo json_encode(array("nama_barang" => "", "harga_barang" => ""));     }  } 

* way not best practice make data base request each of keyup event. can solve.


Comments

Popular posts from this blog

How do you change vbulletin's home page to the forums instead of the default (CMS or Activity Stream)? -

java - stack overflow exception on xml transformation -