【问题标题】:Auto complete textbox for php from MongoDB来自 MongoDB 的 php 自动完成文本框
【发布时间】:2014-04-22 11:04:28
【问题描述】:

这是我的代码

自动完成.php

<html>
<head>
   <script type="text/javascript"
    src="js/jquery-1.4.2.min.js"></script>
    <script type="text/javascript"
    src="js/jquery.autocomplete.js"></script>



     <script type="text/javascript">
            $(document).ready(function(){
                $("#name").autocomplete({
                    source:'test.php',
                    minLength:1,
                    selectFirst: true
                });
            });
      </script>
      </head>

     <body>

       <form method="post" action="">
         Name : <input type="text" id="name" name="name" />
      </form>

     </body>
     <html>

test.php

  <?php
   $my_string =$_GET['q'];
   //echo $my_string;
   syslog($priority, $my_string);
    try {

    // Connect to MongoDB
    $conn = new Mongo('localhost');

    // connect to test database
    $db = $conn->mydb;

    // a new products collection object
    $collection = $db->QuoteCollections;

$quote_array = array(
                    'Author' => $my_string,
                    );        

       $cursor = $collection->find($quote_array);
       //$json=$cursor;

   foreach ($cursor as $obj)
    {
         //$json[]=array(
                //'value'=> $student["Author"],
                //'label'=>$student["Author"]
                   // );
    echo 'Name: ' . $obj['Author'] . "\r\n<br>";
    echo $row['Author']."\n";
    }

      //echo json_encode($json);




        // close connection to MongoDB
         $conn->close();

  }
  catch ( MongoConnectionException $e )
  {
    // if there was an error, we catch and display the problem here
    echo $e->getMessage();
    echo "error at mongoconexe";
  }
 catch ( MongoException $e )
 {
    echo $e->getMessage();
    echo "error at mongoconeException";
  }

?>

我无法得到结果,我参考了这些链接

http://phpseason.wordpress.com/2013/02/13/php-autocomplete-tutorial-using-jquery/

http://tutsforweb.blogspot.in/2012/05/auto-complete-text-box-with-php-jquery.html

我将两个 js 文件放在一个 js 文件夹中

请帮帮我

谢谢

【问题讨论】:

    标签: javascript php jquery wordpress mongodb


    【解决方案1】:

    我终于明白了,

    <!DOCTYPE html>
    
    <html>
    <head>
        <meta charset="UTF-8">
        <title>AutoComplete for Mongo with php</title>
    </head>
    <body>
        <?php
    
        try {
        // Connect to MongoDB
        $conn = new Mongo('localhost');
    
        // connect to test database
        $db = $conn->mydb;
    
        // a new products collection object
        $collection = $db->QuoteCollections;
    
    // the array of product criteria
        $query = $collection->find(array(),array("Author"=>1)); ?>
        <label for="brgy">select:</label>
        <input type="text" list="datlist" autocomplete="off" id="brgy">
        <datalist id ="datlist">
    <?php        foreach($query as $result) 
            {  
                //echo $result['Author'];  ?>
            <option value="<?php echo $result['Author']; ?>"></option>
    
            <?php
            }  ?>
          </datalist>
    
          <?php
    
    
    
        // close connection to MongoDB
        $conn->close();
    
       }
         catch ( MongoConnectionException $e )
         {
        // if there was an error, we catch and display the problem here
        echo $e->getMessage();
        echo "error at mongoconexe";
    }
     catch ( MongoException $e )
    {
        echo $e->getMessage();
        echo "error at mongoconeException";
    }
        ?>
    </body>
    </html>
    

    在 Firefox 中对我来说工作得很好

    【讨论】:

      猜你喜欢
      • 2018-04-03
      • 1970-01-01
      • 2011-04-18
      • 2010-09-22
      • 2015-09-25
      • 2020-05-17
      • 2017-09-01
      相关资源
      最近更新 更多