jquery自动完成响应作为数组如何调用两个字段,如名称和照片

时间:2016-12-02 05:37:30

标签: php html arrays ajax

enter image description here这是输入类型为text的表单字段。

<span>
<img  src="images/author2.jpg" width="50" />  //in Database i have profilepic/userimage.jpg and the image shown in above is a static image.
<input class="searchStudent" type="text" autocomplete="off">
</span>

我已输入字母“A”,响应将以数组形式出现。我想显示用户的照片和名称,我该怎么做...?

这是我从脚本中获取详细信息:

/*Search Student starts here*/
$(document).on("focus keyup", "input.searchStudent", function (event) {
    $(this).autocomplete({
        source: 'gdcontroller.php?action=search',
        select: function (event, ui) {
            event.preventDefault();
            this.value = ui.item.label;
        },
        focus: function (event, ui) {
            event.preventDefault();
            this.value = ui.item.label;
        }

    });
});
/*Search Student ends here*/

这是我的控制器,我在这里搜索名为“A”的学生 并提取他们的详细信息:

if($_GET['action']=="search" && $_GET['term']!='')
{
    $keysearch = $_GET['term'];
    $studentValue = trim($_GET['studentname']);

    $studentsQuery =$conn->query('select s.student_pid,i.email,s.student_email,s.student_fname,s.student_lname,s.profile_pic from r_job_invitations i 
    LEFT JOIN tbl_students s ON i.email = s.student_email where i.id_job =54 and accounttype = 1 and inv_res = 1 and student_fname LIKE "'.$keysearch.'%" OR student_lname LIKE "'.$keysearch.'%" ')or die(mysqli_error());

    $studentData = array();
    while($student = $studentsQuery->fetch_assoc()){
        $studentData[]= $student;
    }
    echo json_encode($studentData);
    exit;
}

1 个答案:

答案 0 :(得分:0)

<div id="photoContainer"></div>

$(document).on("focus keyup", "input.searchStudent", function(event) {
    $(this).autocomplete({
        source: 'gdcontroller.php?action=search',
        select: function (event, ui) {
            event.preventDefault();
            //I'm having trouble here; even name is not coming:
            student.student_fname.value = ui.item.student.student_fname;
            $("#photoContainer").append('<img src="' + ui.item.student.profile_pic + '">'); 
        },
        focus: function(event, ui) {
            event.preventDefault();
            this.value = ui.item.label;
        }    
    });
});