doufusi2013 2017-03-03 05:49 采纳率: 0%
浏览 35
已采纳

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建