2016-03-02 48 views
0

编辑使用PHP和MySQL中获取数据的jQuery UI availableTag

的脚本现在是:

<script> 
$('#tag').keyup(function() { 
    console.log($(this).val()); 
    var termToSearch = $(this).val(); 
    $(function() { 
    var availableTags; 
    $.ajax({ 
     url: 'search_patient.php', 
     type: 'POST', 
     data: {term: termToSearch}, 
     dataType: 'JSON', 

     success:function(output) 
     { 
     $.each(output, function(key, row) 
     { 
      availableTags = [row['patient_name']]; 
     }); 
     $("#tags").autocomplete({ 
     source: availableTags 
    }); 
     } 

    }); 
    }); 
    }); 
    </script> 

我可以在控制台中值看,但还没有看到任何自动完成上搜索文本框。

编辑完

我想使用jquery UI库的自动完成功能,但使用PHP和MySQL填充的数组。

我开始用PHP和MySQL的代码,我需要根据我在搜索框中我打字获得病人的名字(实时自动完成搜索)

<?php 
//Set error reporting on 
error_reporting(E_ALL); 
ini_set("display_errors", 1); 

//Include connection file 
require_once('../include/global.php'); 
//Json and PHP header 
header('Content-Type: application/json'); 
//Getting Value from text box 
$term = '%'.$_POST['term'].'%'; 

//Array to get data into it 
$response = array(); 

//Query 
$searchPatient = "SELECT * FROM patient WHERE patient_name LIKE :term"; 
$searchStmt = $conn->prepare($searchPatient); 
$searchStmt->bindValue(":term", $term); 
$searchStmt->execute(); 
if($searchStmt->rowCount() > 0){ 
    $output = $searchStmt->fetchall(); 
    foreach ($output as $o){ 
     $response['patient_name'] = $o['patient_name']; 
    } 
     return json_encode($response); 
} 
?> 

在我包括在该页面jQuery的UI库,并根据他们的建议,他们用的是以下几点:

<script src="../include/jquery-1.12.1.min.js"></script> 
<script src="../include/jquery-ui.min.js"></script> 
<script> 
    $(function() { 
    var availableTags = [ 
     "ActionScript", 
     "AppleScript", 
     "Asp", 
     "BASIC", 
     "C", 
     "C++", 
     "Clojure", 
     "COBOL", 
     "ColdFusion", 
     "Erlang", 
     "Fortran", 
     "Groovy", 
     "Haskell", 
     "Java", 
     "JavaScript", 
     "Lisp", 
     "Perl", 
     "PHP", 
     "Python", 
     "Ruby", 
     "Scala", 
     "Scheme" 
    ]; 
    $("#tags").autocomplete({ 
     source: availableTags 
    }); 
    }); 
    </script> 

我无法弄清楚如何使用$.ajax摆脱PHP的$response阵列,并把它作为availableTag = response.patient_name

我把它编辑成:

 <script> 
    $(function() { 
    var availableTags; 
    var searchTerm = $("#tag").val(); 
    $.ajax({ 
     url: 'search_patient.php', 
     data: {term: searchTerm}, 
     type: 'POST', 
     dataType: 'JSON', 

     success:function(response) 
     { 
     $.each(response, function(key, row) 
     { 
      availableTags = row['patient_name']; 
     }); 
     $("#tags").autocomplete({ 
     source: availableTags 
    }); 
     } 
    }); 

    }); 
    </script> 

我不得不在XHR term为空:

enter image description here

而且我有这样的错误:

Notice: Undefined index: term in C:\wamp\www\dentist\pages\search_patient.php on line 13

编辑Covic

enter image description here

+0

尝试使用alert(searchTerm)调试'searchTerm'在ajax之前;' – Covik

+0

当它加载页面时我收到一个空警报。但是当我在搜索框中键入内容时,我没有收到任何提醒 – androidnation

+0

您需要在'#tag'元素上监听'keyup'事件。 – Covik

回答

1

我想你应该得到所有患者无term。您可以在服务器端创建JS数组,但也可以使用AJAX完成。

<?php 
//Set error reporting on 
error_reporting(E_ALL); 
ini_set("display_errors", 1); 

//Include connection file 
require_once('../include/global.php'); 
//Json and PHP header 
header('Content-Type: application/json'); 


//Query 
$searchPatient = "SELECT patient_name FROM patient"; 
$searchStmt = $conn->prepare($searchPatient); 
$searchStmt->execute(); 
if($searchStmt->rowCount() > 0){ 
    $output = $searchStmt->fetchall(); 
    $output = array_values($output); 
    echo json_encode($output); 
} 
?> 

现在AJAX我们不需要后期数据

<script> 
    $(function() { 
    var availableTags = []; 
    $.ajax({ 
     url: 'search_patient.php', 
     type: 'POST', 
     dataType: 'JSON', 

     success:function(response) 
     { 
     $.each(response, function(key, row) 
     { 
      availableTags.push(row['patient_name']); 
     }); 
     $("#tags").autocomplete({ 
     source: availableTags 
    }); 
     } 
    }); 

    }); 
    </script> 

也许我做错了什么事,因为我不能测试它现在这样,如果有任何错误,我会解决它

+0

我可以看到数组结果在XHR但在搜索文本框中没有任何内容显示为自动完成 – androidnation

+0

您能提供XHr结果的图像吗?在调用自动​​完成之前,请尝试提醒'availableTags'。尝试将'#tags'改为'#tag' – Covik

+0

等待我将在问题中上传一张图片给您先生 – androidnation

0

,我们可以在许多方面做到这一点。在上述

每个拿到它就像

var availableTags = [ 
    "ActionScript", 
    "AppleScript", 
    "Asp", 
    "BASIC", 
    "C", 
    "C++", 
    "Clojure", 
    "COBOL", 
    "ColdFusion", 
    "Erlang", 
    "Fortran", 
    "Groovy", 
    "Haskell", 
    "Java", 
    "JavaScript", 
    "Lisp", 
    "Perl", 
    "PHP", 
    "Python", 
    "Ruby", 
    "Scala", 
    "Scheme" 
]; 

我在你的foreach意味着格式

$copy = $output; 

foreach ($output as $o) 
{ 
    echo ' " '.$o. ' " '; 
    if (next($copy)) { 
    echo ','; // Add comma for all elements instead of last 
    $response['patient_name'] = $o['patient_name']; 
} 
} 
return $response; 

并将其分配给JavaScript变量,如

availableTags = [response]; 

希望帮助:)

+0

我会试试看。但首先,没有任何内容被发送到PHP代码。在XHR我有这个词是空的 – androidnation

+0

然后试试这个http://stackoverflow.com/questions/5077409/what-does-autocomplete-request-server-response-look-like – Shiva