2017-03-03 20 views
0

我的編碼是所有關於ALERT,當搜索詞不是在MySQL數據庫中找到

1)從MySQL獲取數據救援人員到場PHP

2)從PHP獲取數據使用PHP URL D3基於對輸入 我想設置的警報時,在輸入字段中的文本在MySQL數據庫中找不到..

現在

當我嘗試用比MySQL數據之外的字,它顯示

此控制檯

enter image description here

我怎麼可以提醒時,錯一個字(比MySQL數據庫值等)被提交

HTML表單

 <form name="editorForm"> 
     <input type="text"name="editor" id="editor" 
     onchange="document.getElementById('editorForm').submit();"> 
     <input type="submit"value="butn"> 
     </form> 

jQuery來來去PHP根據數據URL

 $(function() { 
     $('form').submit(function (e) { 
     e.preventDefault(); 
     var t=$('form').serialize(); 
     var u='http://localhost:8888/saff/indexi.php?'+t; 
     if(u==null){ 
     alert("not found"); 
     } 
     else{   
     funn(); 
     } 

D3碼

 function funn(){ 
     d3.json(u, function(treeData) { 
      //D3 CODES 
      }); 
       } 

我的PHP代碼

  <?php 
      $con=mysqli_connect("localhost","root","admin","data");  

      if (mysqli_connect_errno())     
       { 
       echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
       } 
       $name=$_GET['editor']; 
      $sql="SELECT * FROM phptab where value LIKE '%".$name."%'"; 
      $r = mysqli_query($con,$sql); 
        $data = array(); 

        while($row = mysqli_fetch_assoc($r)) { 
         $data[] = $row; 
        } 

      function buildtree($src_arr, $parent_id = 0, $tree = array()) 
      { 
       foreach($src_arr as $idx => $row) 
       { 
        if($row['parent'] == $parent_id) 
        { 
         foreach($row as $k => $v) 
          $tree[$row['id']][$k] = $v; 
         unset($src_arr[$idx]); 
      $tree[$row['id']]['children'] = buildtree($src_arr, $row['id']); 
        } 
       } 
       ksort($tree); 
       return $tree; 
      } 

      function insertIntoNestedArray(&$array, $searchItem){ 

       if($searchItem['parent'] == 0){ 
        array_push($array, $searchItem); 
        return; 
       } 
       if(empty($array)){ return; } 
      array_walk($array, function(&$item, $key, $searchItem){ 
       if($item['id'] == $searchItem['parent']){ 
         array_push($item['children'], $searchItem); 
         return; 
        } 
        insertIntoNestedArray($item['children'], $searchItem); 
      }, $searchItem); 
      } 
      $nestedArray = array(); 
      foreach($data as $itemData){ 
      //$nestedArrayItem['value'] = $itemData['value']; 
       $nestedArrayItem['id'] = $itemData['id']; 
       $nestedArrayItem['name'] = $itemData['name']; 
       $nestedArrayItem['parent'] = $itemData['parent']; 
       $nestedArrayItem['tooltip'] = $itemData['tooltip']; 
       $nestedArrayItem['color'] = $itemData['color']; 
       $nestedArrayItem['level'] = $itemData['level']; 

       $nestedArrayItem['children'] = array(); 
      //$data[]=$dat; 
       insertIntoNestedArray($nestedArray, $nestedArrayItem); 
      } 
      header('Content-Type: application/json'); 

      $json= json_encode($nestedArray,JSON_UNESCAPED_UNICODE); 
      echo $json = substr($json, 1, -1); 
       ?> 

按預期工作時所使用的詞是在數據庫 並在Mozilla的page looks like this 得到正確的JSON格式存在console.but設計不顯示在頁面中...但在鉻,一切工作正常..

+0

等待什麼呢?從網址'var u ='http:// localhost:8888/saff/indexi.php?'+ t;'獲取數據?你聽說過阿賈克斯嗎? – madalinivascu

+0

yes.why使用這個url獲取數據時出現任何錯誤 – prog

+0

當我在數據庫中的輸入字段中鍵入單詞時..正確獲取數據,沒有問題...當我鍵入不在它顯示的數據庫在控制檯JSON輸入的意外結束...但我想提醒「未找到」.. – prog

回答

0

您需要測試頁面是否在D3

function funn(){ 
     d3.json(u, function(treeData) { 
      if(!treeData.length){ 
       alert("not found"); 
      }else { 
      //D3 CODES 
      } 
      }); 
      } 

的JSON功能空確保您從頁面返回一個空的對象時未找到