2012-10-30 35 views
0

的信息,這我使用的第一個文本框使用jquery自動填充一個文本框。該文本框稱爲「producto1」自動填充我有一個表格前面的文本框

我想要的是,當用戶寫「producto1」信息自動加載「marca1」內容,在這一領域的內容會從我的數據庫加載。

這是我的代碼:

<table width="100%" border="0" align="center" cellpadding="5" cellspacing="0"> 
      <tr> 
       <td width="545"><div align="center"> 
       <input name="producto1" type="text" class="textfield4" id="producto1" value="" size="65" /> 
       </div></td> 
       <td width="385"><div align="center"> 
       <input name="marca1" type="text" class="textfield5" id="marca1" size="10" maxlength="5" onKeyPress="return handleEnter(event);"> 
       </div></td> 
       <td width="385"><input name="cantidad1" type="text" class="textfield5" id="textfield2" size="10" maxlength="5" onKeyPress="return handleEnter(event);"></td> 
      </tr> 
</table> 

,將在「marca1」字段中顯示會從我的數據庫加載,但我的問題是,我不知道如何填寫的信息自動使用ajax這個第二個字段。

回答

0
// JS File 
$(document).ready(function(){ 
    $("#producto1").keyup(function(){ 
     $.post('/path-to/marca.php', 
       { 
       text: $(this).val() 
       }, 
       function(data) { 
        $('#marca1').val(data.returns); 
       }, 
       'json'); 
    }); 
}); 

// PHP File 

$sql = $mysqli->query("SELECT marca_name 
         FROM marca 
         WHERE marca_name 
         LIKE '%" . $mysql->real_escape_string($_POST['text']) . "%'"); 
while($row = $sql->fetch_assoc($sql)) 
    $returns[] = $row['marca_name']; 

echo json_encode("returns" => implode(",", $returns));