2017-05-24 121 views
1

我想在暱稱搜索欄中添加自動填充功能。我不明白爲什麼它不起作用。我的代碼是正確的?JQuery UI Autocomplete Wordpress

在我的文件liste.php

global $wpdb; 
    $name = $_POST['code_postal']; 
    $sql = $wpdb->get_results("SELECT * FROM membres WHERE pseudo LIKE '$name%' "); 

    $titles = array(); 
    foreach($sql as $key=> $value){ 
     echo $value->pseudo; 
    } 

    echo json_encode($titles); //encode into JSON format and output 

在我global.js

$('#recherche').autocomplete({ 
    source: function(name, response) { 
     $.ajax({ 
      type: 'POST', 
      dataType: 'json', 
      url: 'wp-content/themes/ARLIANE/liste.php', 
      data: 'action=get_listing_names&name='+name, 
      success: function(data) { 
       response(data); 
      } 
     }); 
    } 
}); 

在我的index.php

<form> 
 
    <input type="text" name="term" id="recherche"/> 
 
</form>

+0

Typo in liste.php [...] membErs [...]? – Felix

+0

對不起,我不明白... – Thomas

+0

我猜你想訪問表「成員」,但正在嘗試「membres」這將是一個拼寫錯誤 – Felix

回答

0

你可以嘗試登錄AJAX錯誤控制檯有關該問題的詳細信息,像這樣:

 $.ajax({ 
     type: 'POST', 
     dataType: 'json', 
     url: 'wp-content/themes/ARLIANE/liste.php', 
     data: 'action=get_listing_names&name='+name, 
     success: function(data) { 
      response(data); 
     }, 
     error: function(xhr, textStatus, error){ 
     console.log(xhr.statusText); 
     console.log(textStatus); 
     console.log(error); 
     } 
     }); 
0

嘗試通過添加頁面來改變你的js代碼的URL和文件關聯liste.php作爲自定義頁面類型,不是添加的頁面網址:

url: '<?php echo get_permalink(page_id); ?>', 
0

如果我做了包括liset.php這個返回我還有一個表JSON。 但是,當我讓我的ajax調用它返回一個錯誤500.

我認爲問題來自與自動完成功能的jquery調用。