dragonsun00000 2014-04-22 11:04
浏览 32
已采纳

从MongoDB自动完成php的文本框

Here is my code

autocomplete.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'] . "
<br>";
    echo $row['Author']."
";
    }

      //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";
  }

?>

i cannot get the result, i referd these links

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

i placed both js files inside a js folder

please help me

Thanks

  • 写回答

1条回答 默认 最新

  • donglang1894 2014-04-23 12:07
    关注

    finally i figured it out,

    <!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>
    

    worked fine for me in firefox

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

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏