2016-07-13 5 views
-1

У меня возникла проблема с плагинами автозаполнения JQuery UI с Ajax.JQuery UI автозаполнение с Ajax начинается с

Он работает локально, но он не всегда работает на сервере, потому что консоль дает мне «Uncaught TypeError: Невозможно прочитать свойство« split »из null» всякий раз, когда я использую только один символ или вообще никакого символа. он работает только с 2-х символов ... его действительно странно действительно

Это мои файлы

ajax.php:

<?php 
require_once 'config.php'; 
if(!empty($_POST['type'])){ 
    $type = $_POST['type']; 
    $name = $_POST['name_startsWith']; 
    $query = "SELECT id_pro, name, IFNULL(SUM(qty),0) as qty FROM inventory where ".$type." LIKE '%".$name."%' group by id_pro"; 
    $result = mysqli_query($con, $query); 
    $data = array(); 
    while ($row = mysqli_fetch_assoc($result)) { 
     $name = $row['id_pro'].'|'.$row['name'].'|'.$row['qty']; 
     array_push($data, $name); 
    } 

    echo json_encode($data);exit; }?> 

auto.js

$(document).on('focus','.autocomplete_txt',function(){ 
type = $(this).data('type'); 

if(type =='id_pro')autoTypeNo=0; 
if(type =='name')autoTypeNo=1; 


$(this).autocomplete({ 
    source: function(request, response) { 
     $.ajax({ 
      url : 'ajax.php', 
      dataType: "json", 
      method: 'post', 
      data: { 
       name_startsWith: request.term, 
       type: type 
      }, 
      success: function(data) { 
       response($.map(data, function(item) { 
        var code = item.split("|"); 
        return { 
         label: code[autoTypeNo], 
         value: code[autoTypeNo], 
         data : item 
        }; 
       })); 
      } 
     }); 
    }, 
    autoFocus: true,    
    minLength: 0, 
    select: function(event, ui) { 
     var names = ui.item.data.split("|");       
     id_arr = $(this).attr('id'); 
     id = id_arr.split("_"); 
     $('#itemNo_'+id[1]).val(names[0]); 
     $('#itemName_'+id[1]).val(names[1]); 
     $('#stock_'+id[1]).val(names[2]); 
     $('#quantity_'+id[1]).val(1); 

     }    
}); }); 

despacho.php

<td><input class="case" type="checkbox"/></td> 
    <td><input type="text" data-type="id_pro" name="itemNo[]" id="itemNo_1" class="form-control autocomplete_txt changesNo" autocomplete="off" required></td> 
    <td><input type="text" data-type="name" name="itemName[]" id="itemName_1" class="form-control autocomplete_txt changesNo" autocomplete="off" required></td> 
    <td><input type="number" name="stock[]" id="stock_1" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" readonly></td> 
    <td><input type="number" min="1" name="quantity[]" id="quantity_1" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" required></td> 

ответ

0

После looooot поиска в Интернете, я понимаю проблему. Я получал нулевые значения для всех полей с метками акцента. решение было простым

ajax.php:

<?php 
require_once 'config.php'; 
if(!empty($_POST['type'])){ 
    $type = $_POST['type']; 
    $name = $_POST['name_startsWith']; 
    $query = "SET NAMES utf8"; 
    $result = mysqli_query($con, $query); 
    $query = "SELECT id_pro, name, IFNULL(SUM(qty),0) as qty FROM inventory where ".$type." LIKE '%".$name."%' group by id_pro"; 

    $result = mysqli_query($con, $query); 
    $data = array(); 
    while ($row = mysqli_fetch_assoc($result)) { 
     $name = $row['id_pro'].'|'.$row['name'].'|'.$row['qty']; 
     array_push($data, $name); 
    } 

    echo json_encode($data);exit; 
} 
?> 
Смежные вопросы