jquery自动完成标签问题

时间:2016-12-02 08:46:55

标签: php jquery mysql ajax autocomplete

enter image description here

输入1字符时,显示空白,标签在自动完成时缺失。如何设置自动完成的标签。但价值观正在到来。 这是带有输入类型文本的表单字段。

<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;
}

2 个答案:

答案 0 :(得分:3)

尝试此函数mysql_real_escape_string()

答案 1 :(得分:2)

您的回复应该是一个JSON对象(数组),其中每个项目都是idlabelvalue个键的对象。

$studentData数组中的每个项都应包含这些键,以便自动填充显示数据。

我不确定您要显示哪些内容,但您可以尝试以下方式:

    while($student = $studentsQuery->fetch_assoc()){
        $student['id'] = $student['student_pid'];
        $student['label'] = $student['student_fname'];
        $student['value'] = $student['student_fname'];
        $studentData[]= $student;
    }

您应该使用这些值来显示您想要的内容。

相关问题