爆炸返回未定义的偏移量:0并且可能会破坏ajax响应

时间:2018-04-21 09:47:28

标签: php jquery ajax

当我满足条件时,我正在使用JQuery添加输入字段。这个输入字段应该作为实时搜索,所以我在keyup事件上附加了它,必须通过ajax发送php侧页中字段的值,页面应检查与给定单词或字母匹配的数据库结果,并以json对象的形式返回结果。

但问题在于我的爆炸正在给PHP注意:未定义的偏移量:0 in ...从那里我的Ajax返回没有从文本到对象的转换。并没有在DOM或控制台中打印任何内容,但在网络选项卡中我得到了我想要的东西......它真的让人困惑,一步一步:

$('#intake_goal_main').after('<input type = "text" name = "intake-search-field " class = "intake-search-field"/>');
    
    
    $('.intake-search-field').on('keyup',function(){
        var intake_search_product_name = $(this).val();
    $.ajax({
        type:     "POST",
        data: {'product_name':intake_search_product_name},
        cache:    false,
        url:      '/ajax/intake/intake_search.php',
        dataType: JSON,
        success:        function(result){
            
            $('.intake-food-result').remove();
            
           // $('.intake-search-field').after('<div class = "intake-food-result"></div>').html(result);
            
           
        },
        error: function(jqXHR, textStatus, errorThrown) {
        alert(jqXHR.status);
        alert(textStatus);
        alert(errorThrown);
    }
    })
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="intake_goal_main"><select id="intake-pick-goal"><option>-- Изберете цел --</option><option value="intake-bulk">Покачване на килограми</option><option value="intake-cut">Сваляне на килограми</option><option value="intake-maintenance">Поддържане на килограми</option></select> </section>
<?php

if(!empty($_POST['product_name'])) {


  $product_name  =$_POST['product_name'];
  $req = ("SELECT * FROM food_data_bg WHERE ");
  $term = explode(" ",$product_name); //here is the problem...!!!!
  $i=0;

foreach($term as $form){
     $i++;
    if($i == 1){
    
       $req .= "title LIKE '%".$form."%'";
       
    }else{
    
       $req .= "OR title LIKE '%".$form."%'";
    }
    
}


require_once $_SERVER['DOCUMENT_ROOT'].'/inc/connection.php';
  $req = mysqli_query($connect,$req);
  $num_rows = mysqli_num_rows($req);

  if($num_rows == 0){
  
    $no_result = 'Няма резултати';
    echo json_encode($no_result);
    exit();
    
  }

else{
   $result = array();
   
  while($row = mysqli_fetch_assoc($req)){
    $title= $row["title"];
    $myID= $row["id"];
    $image = $row["fimage"];
    $state = $row["state"];

      $result[] = array(
          'title' => $title,
          'id'    => $myID,
          'image' => $image,
          'state' => $state

          );
       }
       echo json_encode($result);
       exit();
    }
}

?>

还有我的网络标签:

我很抱歉很长时间的问题,我很困惑,为什么不是我的工作。所以简而言之:

- 为什么爆炸会在...中返回未定义的索引:0?如果您需要更多信息,请询问!谢谢!

0 个答案:

没有答案