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 を使用してこの 2 番目のフィールドを自動的に入力する方法がわからないことです。

4

1 に答える 1

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));
于 2012-10-30T06:27:31.773 に答える