【问题标题】:Auto populate a textfield with the information of previous textfield使用前一个文本字段的信息自动填充文本字段
【发布时间】:2012-10-30 06:18:09
【问题描述】:

我有一个表单,我在第一个文本字段中使用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 自动填充第二个字段。

【问题讨论】:

    标签: php mysql ajax textfield


    【解决方案1】:
    // 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));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      相关资源
      最近更新 更多