2012-10-30 33 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));