基于使用PHP和MySQL的一个字段jQuery的自动完成两个字段 [英] Jquery autocomplete two fields based on one field using PHP and MySQL

查看:118
本文介绍了基于使用PHP和MySQL的一个字段jQuery的自动完成两个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MySQL数据库的表列如下:姓名,地址,职业,PHONE_NUMBER和SERIAL_NUMBER。在我的HTML表单,其他领域中,我对输入姓名,电话号码和序列号的文本字段。我如何使用序列号字段使用jQuery自动完成和PHP / MySQL的填充姓名和电话号码字段?我已经成功地设置自动填充在一个输入框,但我不知道如何设置它来填充使用具有自动完成一个领域的另外两个领域。谢谢

这些领域

 <输入类型=文本名称=名字ID =自动完成/>
   <输入类型=文本名称=PHONE_NUMBERID =自动完成/>
   <输入类型=文本名称=SERIAL_NUMBERID =自动完成/>

这是我的PHP脚本获取从数据库字符串

 < PHP
$ DBHOST ='YOUR_SERVER';
$ DBUSER ='YOUR_USERNAME';
$ DBPASS ='YOUR_PASSWORD';
$ DBNAME ='YOUR_DATABASE_NAME';$康恩= mysql_connect($ DBHOST,$ DBUSER,$ DBPASS)或死亡(错误连接到MySQL');
mysql_select_db($数据库名);$ return_arr =阵列();如果($康恩)
{
    $取=的mysql_query(SELECT姓名,PHONE_NUMBER,SERIAL_NUMBER FROM用户);
    而($行= mysql_fetch_array($取,MYSQL_ASSOC)){
        $ row_array ['值'] = $行[名称。 。 。$行['SERIAL_NUMBER']$行['PHONE_NUMBER'];
        array_push($ return_arr,$ row_array);
    }
}
mysql_close($康恩);
回声json_en code($ return_arr);
?>


解决方案

你需要做的是,当序列号的自动完成功能完成后,运行一个AJAX调用基于串行检索姓名和电话号码号码,然后填充这些领域,这样的事情:

  $('输入[名称= SERIAL_NUMBER]')。自动完成({
    的onComplete:功能(VAL){
        $ .getJSON('/ URL到取的名称和-phone.php?SERIAL_NUMBER ='+ VAL,功能(数据){
            $(输入[名称=名称])VAL(data.name)。
            $(输入[名称=联系电话]')VAL(data.tel)。
        });
    }
});

显然,你需要根据您的设​​置一些函数和变量名来改变。

I have a table in MySQL database with following columns: name, address, occupation, phone_number and serial_number. In my html form, among other fields, I have text fields for inputting Name, Phone number and Serial number. How can I, using jQuery autocomplete and PHP/MySQL populate the Name and Phone number fields using the Serial number field? I've managed to set autopopulate on one input field, but I don't know how to set it to populate the other two fields using the one field which has autocomplete. Thanks

these are the fields

   <input type="text" name="name" id="autocomplete" />
   <input type="text" name="phone_number" id="autocomplete" />
   <input type="text" name="serial_number" id="autocomplete" />

this is my php script for getting the strings from db

<?php
$dbhost = 'YOUR_SERVER';
$dbuser = 'YOUR_USERNAME';
$dbpass = 'YOUR_PASSWORD';
$dbname = 'YOUR_DATABASE_NAME';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);

$return_arr = array();

if ($conn)
{
    $fetch = mysql_query("SELECT name, phone_number, serial_number FROM users");


    while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
        $row_array['value'] = $row['name'] . " " . $row['phone_number'] " . $row['serial_number'] ";         
        array_push($return_arr,$row_array);
    }


}
mysql_close($conn);
echo json_encode($return_arr);
?>

解决方案

What you need to do is, when the autocomplete of the serial number is finished, run an ajax-call to retrieve the name and phone number based on the serial number and then populate those fields, something like this:

$('input[name=serial_number]').autocomplete({
    onComplete: function (val) {
        $.getJSON('/url-to-fetch-name-and-phone.php?serial_number=' + val, function (data) {
            $('input[name=name]').val(data.name);
            $('input[name=tel]').val(data.tel);
        });
    }
});

Obviously you'll need to change some function and variable names depending on your setup.

这篇关于基于使用PHP和MySQL的一个字段jQuery的自动完成两个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆