JQuery Autocomplete from Database

后端 未结 3 1181
余生分开走
余生分开走 2021-01-01 08:25

I need to to do autocomplete suggestion for my website and the data should be retrieved from database. I want to use JQuery autocomplete. here is my code but it doesn\'t wor

相关标签:
3条回答
  • 2021-01-01 08:31

    Your PHP script should be accepting a term parameter, not q.

    0 讨论(0)
  • 2021-01-01 08:36

    I did some changes, maybe you need to fix something but take a look to see if helps...

    The php:

    <?php
        require_once ('config.php');
    
        $q=$_REQUEST["q"]; 
        $sql="SELECT `fname` FROM `Property` WHERE fname LIKE '%$q%'";
        $result = mysql_query($sql);
    
        $json=array();
    
        while($row = mysql_fetch_array($result)) {
          array_push($json, $row['fname']);
        }
    
        echo json_encode($json);
    ?>
    

    The html+jquery:

    <html>
        <head>
            <script src="//code.jquery.com/jquery-1.10.2.js"></script>
            <script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
            <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />
        </head>
        <body>
            <form class="sansserif" action="view.php" method="post">
                Name: <input type="text" id="hint" name="hint" />
                <input type="submit" name="submit" value="View">
            </form>
    
            <script type="text/javascript"> 
    
            $(function() {
                $( "#hint" ).autocomplete({
                    source: function( request, response ) {
                        $.ajax({
                            url: "gethint.php",
                            dataType: "jsonp",
                            data: {
                                q: request.term
                            },
                            success: function( data ) {
                                response( data );
                            }
                        });
                    },
                });
            });     
            </script>
        </body>
    </html>
    
    0 讨论(0)
  • 2021-01-01 08:53
    <?php
     require_once ('..\config.php');
     $q=$_REQUEST["term"]; 
     $sql="SELECT `fname` FROM `Property` WHERE fname LIKE '%$q%'";
     $result = mysql_query($sql);
    
     while($row = mysql_fetch_array($result)) {
      $json[]=array(
       'value'=> $row['fname'],
       'label'=> $row['fname']
      );
     }
     echo json_encode($json);
    ?>
    

    All I changed was the $_REQUEST["q"] to $_REQUEST["term"].

    0 讨论(0)
提交回复
热议问题