doufusi2013
2017-03-03 05:49
浏览 34
已采纳

ALERT,搜索时在mysql数据库中找不到单词

my coding is all about

1)fetch the data from mysql thro php

2)get data from php to d3 based on input by using PHP URL I want to set alert when the text in the input field is not found in mysql database..

now when I try with the word other than mysql data, it shows

this console

enter image description here

how can i alert when wrong word(other than mysql database value) is submitted

HTML FORM

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

JQUERY TO FETCH THE DATA FROM PHP BASED ON 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 CODES

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

my php code

            <?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);
               ?>

works as expected when the word used is exist in the database and the page looks like this getting correct json format in the mozilla console.but design is not shown in the page...but in chrome ,everything works fine..

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doupo2241 2017-03-03 06:03
    已采纳

    You need to test if the page is empty in the json function of the d3

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

    Make sure that you return a empty object from the page when not found

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题