Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
自动完成不使用php或javascript_Javascript_Php - Fatal编程技术网

自动完成不使用php或javascript

自动完成不使用php或javascript,javascript,php,Javascript,Php,我试图用Javascript和php制作一个简单的自动完成脚本,但它根本不起作用。提前谢谢你的帮助! 这是我的html <!doctype html> <html lang="en"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script src="https:

我试图用Javascript和php制作一个简单的自动完成脚本,但它根本不起作用。提前谢谢你的帮助! 这是我的html

<!doctype html>
<html lang="en">
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="https:ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
    <script src="js/jquery-1.7.1.min.js"></script>
    <script src="js/countries.js"></script>
    <meta charset="utf-8">
    <title>Countries</title>
    <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link rel="stylesheet" href="css/jquery-ui.css">
</head>
<body>
    <div class="ui-widget">
        <label for="country">Country: </label>
        <input type="text" name="country" id="country">
    </div>

</body>
</html>
还有php

<?php // countries.php
header('Content-Type: application/json');
$data = array();
$countries = array( 'Afghaistan', 'Albania', 'Algeria', 'Andorra');
if (isset($_GET['term'])) {
    foreach ($countries as $country) {
        if (stripos($country, $_GET['term']) !== false) $data[] = $country; 
} // End of FOREACH.
} // End of IF.
echo json_encode($data);

从我看到的情况来看,您在PHP文件中使用JSON对结果进行编码,但在jQuery中没有从JSON解码,请尝试使用
jQuery.parseJSON()

为什么要加载jQuery 1.10.2(cdn)和1.7.1?出现了什么错误?您检查过PHP是否显示了正确的JSON吗?当您转到resources/countries.php时,浏览器会显示什么?这里有一个教程可以帮助您。我现在在上大学,完全不知所措,只是在看一本书。代码对代码,但这本书充满了错误,所以我迷路了。谢谢你的帮助!autocomplete需要JSON编码的数据()$。autocomplete自己做这件事。
<?php // countries.php
header('Content-Type: application/json');
$data = array();
$countries = array( 'Afghaistan', 'Albania', 'Algeria', 'Andorra');
if (isset($_GET['term'])) {
    foreach ($countries as $country) {
        if (stripos($country, $_GET['term']) !== false) $data[] = $country; 
} // End of FOREACH.
} // End of IF.
echo json_encode($data);