从jquery自动完成填充表单数据

时间:2014-04-08 19:10:33

标签: php jquery mysql forms populate

想知道是否有人可以查看我的代码并指出我正确的方向。 当您键入名称时,它应自动完成并自动填充部门和电话字段。 如果我删除$ getName = $ _REQUEST ['term']并将其替换为$ getName =“John Doe”,它会起作用,但会失去动态。

Jquery的


        jQuery(document).ready(function(){
            $('#name').autocomplete({
                source:'pop.php', 
                minLength:2,
                select:function(evt, ui)
                {
                    $(this).focusout(function(e) {
                        this.form.dept.value = ui.item.dept;
                        this.form.phone.value = ui.item.phone; 
                        this.form.ext.value = ui.item.ext; 
                    });

                }
            });
        });

HTML表单

<form onsubmit="return false;">
    <input id="name" type="text" name="name"/>
    <input id="dept" type="text" name="dept"/>
    <input id="phone" type="text" name="phone"/>
    <input id="ext" type"text" size="4" name ="ext" maxlength="4" />     
</form>

PHP:      

//Get request variable
$getName = $_REQUEST['term'];

//Explode text field into into variables
list($fname,$lname) = explode(' ', $getName, 2);


$data = array();

//Build query
$sql = "SELECT * FROM contacts WHERE fname LIKE '$fname%' AND lname LIKE '$lname%' LIMIT 10";

//Execute Query
$resultSet = $mysqli->query($sql);

//Loop through the query
while($row = $resultSet->fetch_assoc())
{

    $data[] = array(
        'value' =>$row['fname'] .' '. $lname['lname'],
        'dept'  =>$row['dept'],
        'phone' =>$row['phone'],
        'ext'   =>$row['ext']
    );
}

echo json_encode($data);
 ?>

0 个答案:

没有答案