简体   繁体   English

如何通过PHP页面将数据库值检索到jQuery自动完成框

[英]How to retrieve the database values to jquery auto complete box through php page

I am a newbie to jQuery; 我是jQuery的新手; I want to get the data from a database to auto-complete a text box. 我想从数据库中获取数据以自动完成文本框。

I have coded the PHP to get the values from the database. 我已经对PHP进行了编码,以从数据库中获取值。 How can I get these PHP values in a jQuery page? 如何在jQuery页面中获取这些PHP值?

This is the script: 这是脚本:

 <script>
    $(function() {
        var Theaters = [
            "PVR",
            "SCR",
            "MTR"
        ];
        $( "#tags" ).autocomplete({
            source: Theaters
        });
    });
    </script>

This is the PHP page: 这是PHP页面:

<?php
mysql_connect("localhost", "root") or die (mysql_error ());
mysql_select_db("theaterdb") or die(mysql_error());
$strSQL = "SELECT * FROM theaters";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) 
{
echo $row['theater_name'] . "<br />";
 }
mysql_close();
?>

How can I do this? 我怎样才能做到这一点?

Use ajax for this purpose. 为此使用ajax。

<html>
 <head>
 <title>Untitled Document</title>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
 <script type="text/javascript">
 $(function(){
    $('#radioid').click(function() {
    $('#txtDoorStyle').val($(this).val());
});
 });
 </script>
  </head>
 <body>
 <input type="radio" id="radioid" value="Door Half Transom (011)" name="doorstyle">
 <input type="text" value="" id="txtDoorStyle" name="txtDoorStyle" >
 </body>
 </html>>

you have two choices either two load this autocomplete options upfront in which case you store the data that you have retrieved from the dB in a hidden field after serializing the data to a string and in your javascript you parse it back to an array using str.split(",") 您有两个选择,或者两个都预先加载了此自动完成选项,在这种情况下,将数据串行化为字符串后,将从dB中检索到的数据存储在隐藏字段中,然后在JavaScript中使用str.split(",")将其解析回数组str.split(",")

or 要么

you could make an ajax request when the user starts typing and then retrieve the data as a string and then parse it to an array and then pass it to the autoComplete api. 您可以在用户开始输入内容时提出ajax请求,然后以字符串形式检索数据,然后将其解析为数组,然后将其传递给autoComplete api。 You can read about jquery ajax here 您可以在这里阅读有关jquery ajax的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM