dongwende1984 2015-10-06 16:25
浏览 43

使用PHP和MySQL自动完成的jQuery无法正常工作

Study jQuery from online tutorial, find many tutorials, just find one simple maybe good for newbie. Here is the code for index.html:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>
    Autocompletement    
    </title>
    <link rel="stylesheet" type="text/css" href="style.css"> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="suggest.js"></script>
</head>
<body>

<input type="text" name="suggest" placeholder="Type a Country Name..." onkeyup="suggestion()"/>
<div id="autosuggest"></div>
</body>
</html>

Here is the code for suggest.php:

<?php
include "connect.php";

function auto($data){
    global $mysqli;
    $data = $_GET['data'];
    $query = "SELECT code, name_en 
              FROM countries 
              WHERE name_en LIKE '%$data%' 
              OR code LIKE '%$data%'";

    $items = '<ul class="suggestion">';

    if($result = $mysqli->query($query)){
         /* fetch associative array */
         while($row = $result->fetch_assoc()){
             $items .= '<li>'.$row['name_en'] . ' ' . $row['code'].'</li>';
         }
         $items .= '</ul>';
    }else{
        $items = "No results Found..."; 
    }

    echo $items;
} 

auto();

?>

Here is the code for suggest.js

function suggestion(){
    var suggestVal = $('#suggest').val();

    if(suggestVal != ''){
        $.ajax({
            url: 'suggest.php?data='+suggestVal,
            success: function(data){
                $('#autosuggest').html(data);
            }
        })
    }
}

The result above will only show "No results Found...", it seems that processing file suggest.php is not working, then I test it with a test file test.php:

$mysqli = new mysqli("host","user","pass","database");
//auto();

$data = $_GET['input'];
//$data = "ca";
 $query2 = "SELECT code, name_en FROM countries WHERE name_en LIKE '%$data%' OR code LIKE '%$data%'";

echo "<br>" .$query2;
echo "<br>";
if($mysqli){
    echo "Yes SQL";echo "<br>";
}else{
    echo "No SQL";echo "<br>";
}
$result = $mysqli->query("SELECT code, name_en FROM countries WHERE name_en LIKE '%$data%' OR code LIKE '%$data%'");

if($result->num_rows){
    echo "Yes Result";echo "<br>";
}else{
    echo "No Result";echo "<br>";
}

The weird thing is that I cannot find anything wrong with my code, checked php.net sample and seems all to be good? But when I check vam_dump($result) then it is "null", any helps will appreciated. Here is the test.php output:

SELECT code, name_en FROM countries WHERE name_en LIKE '%ch%' OR code LIKE '%ch%'
Yes SQL
No Result

URL: http://IP/project/jquery/auto_diy/test.php?input=ch

  • 写回答

1条回答 默认 最新

  • duangao8359 2015-10-06 16:44
    关注

    You defined your function as having a $data argument

    function auto($data){...}
    

    but you are calling it without the argument,

    auto();
    

    this will trigger a notice.

    The argument is not needed, since you are already using $data=$_GET['data']; inside your function.

    Then in your ajax call you should change it like so

    $.ajax({
        url: 'suggest.php',
        type: 'get', // this can be omitted because GET is default
        data: 'data='+suggestVal, // or
        // data: {data:suggestVal}
        success: function(data){
            $('#autosuggest').html(data);
        }
    })
    

    Update:

    in test.php output directly the value of $result->num_rows

    print "Rows returned: " . $result->num_rows . "<br>";
    

    if you get zero rows, copy the query and run it in phpmyadmin or a mysql console to verify the data.

    in suggest.php, update this:

    $result = $mysqli->query($query);
    
    if (!$result) {
        print 'Could not execute query';
    }
    else {
        while($row = $result->fetch_assoc()){
            $items .= '<li>'.$row['name_en'] . ' ' . $row['code'].'</li>';
        }
    }
    
    $items .= '</ul>';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作