如何从Mysqli jQuery PHP获取输入的AutoComplete数据

时间:2019-06-14 14:40:50

标签: php mysqli

我正在尝试使用jquery自动完成功能从mysqli获取输入数据,我正在使用WAMP版本3.1.9 php-7.2.18,MySql- 5.7.26,phpMyAdmin- 4.8.5,当我使用WAMP版本2.0时,它工作正常 但是在WAMP版本3.1.9上它不起作用..i我尝试在我的脚本中将mysql更改为mysqli,但仍然无法工作 任何帮助将不胜感激,在此先感谢..

countries.sql

-- phpMyAdmin SQL Dump
-- version 4.8.5
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1:3306
-- Generation Time: Jun 14, 2019 at 01:44 PM
-- Server version: 5.7.26
-- PHP Version: 7.2.18

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `masterdb`
--

-- --------------------------------------------------------

--
-- Table structure for table `countries`
--

DROP TABLE IF EXISTS `countries`;
CREATE TABLE IF NOT EXISTS `countries` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `country_code` varchar(2) NOT NULL DEFAULT '',
  `country_name` varchar(100) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=51 DEFAULT CHARSET=utf8;

--
-- Dumping data for table `countries`
--

INSERT INTO `countries` (`id`, `country_code`, `country_name`) VALUES
(2, 'CA', 'Canada'),
(3, 'AF', 'Afghanistan'),
(4, 'AL', 'Albania'),
(5, 'DZ', 'Algeria'),
(6, 'DS', 'American Samoa'),
(7, 'AD', 'Andorra'),
(8, 'AO', 'Angola'),
(9, 'AI', 'Anguilla'),
(10, 'AQ', 'Antarctica'),
(11, 'AG', 'Antigua and/or Barbuda'),
(12, 'AR', 'Argentina'),
(13, 'AM', 'Armenia'),
(14, 'AW', 'Aruba'),
(15, 'AU', 'Australia'),
(16, 'AT', 'Austria'),
(17, 'AZ', 'Azerbaijan'),
(18, 'BS', 'Bahamas'),
(19, 'BH', 'Bahrain'),
(20, 'BD', 'Bangladesh'),
(21, 'BB', 'Barbados'),
(22, 'BY', 'Belarus'),
(23, 'BE', 'Belgium'),
(24, 'BZ', 'Belize'),
(25, 'BJ', 'Benin'),
(26, 'BM', 'Bermuda'),
(27, 'BT', 'Bhutan'),
(28, 'BO', 'Bolivia'),
(29, 'BA', 'Bosnia and Herzegovina'),
(30, 'BW', 'Botswana'),
(31, 'BV', 'Bouvet Island'),
(32, 'BR', 'Brazil'),
(33, 'IO', 'British lndian Ocean Territory'),
(34, 'BN', 'Brunei Darussalam'),
(35, 'BG', 'Bulgaria'),
(36, 'BF', 'Burkina Faso'),
(37, 'BI', 'Burundi'),
(38, 'KH', 'Cambodia'),
(39, 'CM', 'Cameroon'),
(40, 'CV', 'Cape Verde'),
(41, 'KY', 'Cayman Islands'),
(42, 'CF', 'Central African Republic'),
(43, 'TD', 'Chad'),
(44, 'CL', 'Chile'),
(45, 'CN', 'China'),
(46, 'CX', 'Christmas Island'),
(47, 'CC', 'Cocos (Keeling) Islands'),
(48, 'CO', 'Colombia'),
(49, 'KM', 'Comoros'),
(50, 'CG', 'Congo'),
(1, 'US', 'United States');
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

index.php

<!doctype html>
<html lang="en">
<head>
<title>Autocomplete Demo</title>


<script type="text/javascript">
$(function() {
    $("#Txt").autocomplete({
        source: "AutoComplete.php",
        minLength: 1
    });       
});

</script>



</head>


<div class="jumbotron">
  <div class="container">
    <form action="search.php" method="POST" >
        <p>Enter Contry name <br/><input type='text' name='CountryName' value='Txt' id='Txt' class='Txt'></p>
    <input type="submit" value="Search">
        </form>
  </div>
</div>

  </div>
</div>
</body>
</html>

dbConfig.php

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Database Connection Failed : ' . mysqli_error());
}
mysqli_select_db('masterdb');
?>

AutoComplete.php

<?php
include 'dbConfig.php';

$Txt = $_GET['term'];


if (isset($companyTxt)&&($tostation)){
    $dbArray = array();
    $Txt = mysqli_real_escape_string($Txt);

    $query=mysqli_query("SELECT*FROM countries WHERE country_Code like '$Txt%' OR country_name like '$Txt%'  LIMIT 15");    
    while($rows = mysqli_fetch_array($query)){

        $dbArray[] =array('value'=>$rows['country_code'],
                  'label'=>$rows['country_name']. " | " .$rows['country_code']);
        }
    echo json_encode($dbArray);
}


?>

0 个答案:

没有答案