【问题标题】:Auto fill city after typing in zip code输入邮政编码后自动填写城市
【发布时间】:2015-01-03 23:45:51
【问题描述】:

我需要在输入邮政编码时自动填写 CITY: 表格, 它说“未定义的变量:在线数组..” where value="

有人可以帮忙吗?

http://i62.tinypic.com/hv5fkl.jpg

<div id="search">
                        <form name="input" action="" method="get">
                            POST:<input type="text" name="postcode"><br><br>
                            City:<input type="text" name="navn" value="<?php $array['navn'] ?>"><br><br>
                            <input type="submit" value="search">
                        </form> 
                    </div>


                    </div>

                   <?php
                        if(isset($_GET['postcode'])){


                        $postcode = $_GET['postcode'];

                        $content = file_get_contents('http://oiorest.dk/danmark/postdistrikter/'. $postcode . '.json');

                        $array = json_decode($content, true);


                            echo $array['navn'];
                            echo "<br>";


                        }
                    ?>

【问题讨论】:

  • 使用$array[$_GET['navn']];获取值
  • 在我的输入表单中?自动填充会有帮助吗?

标签: php json autofill zipcode city


【解决方案1】:

您希望始终初始化变量。发生的情况是您正在尝试访问尚未初始化的变量。

<?php

$city = ''; // initialize containers
$postcode = '';

if(isset($_GET['postcode'])){
    $postcode = $_GET['postcode'];
    $content = file_get_contents('http://oiorest.dk/danmark/postdistrikter/'. $postcode . '.json');
    $array = json_decode($content, true);
    // when the request is made, then assign
    $city = $array['navn'];
}

?>

<!-- so that when you echo, you'll never worry about undefined indices -->
<div id="search">
    <form name="input" action="" method="get">
    POST:<input type="text" name="postcode" value="<?php echo $postcode; ?>"><br><br>
    City:<input type="text" name="navn" value="<?php echo $city; ?>"><br><br>
    <input type="submit" value="search">
    </form>
</div>

在这个答案中,发生的情况是,在第一次加载时(还没有 json 请求),值是空的,但它们是在顶部声明的。

当您提交表单时,会发生变量赋值并将值替换到该容器中。

Simple Demo

【讨论】:

  • Niceee,谢谢!唯一的问题是输入邮政编码时它不是(自动填写)城市表格
  • @IPastuhI 自动填充是什么意思?这就是浏览器的工作
  • 我的意思是,当我输入邮政编码(例如 5250)并且不点击搜索按钮时,它会找到城市名称
  • @IPastuhI 那不是自动填充,这就像自动完成行为,你需要一些 jquery 自动完成 ajax 来做到这一点。好吧,这只是另一个问题
  • 明白了,哪一个最适合这份工作?您能帮我解决这个问题,以便我将您的答案标记为正确吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-14
  • 1970-01-01
  • 1970-01-01
  • 2014-06-16
相关资源
最近更新 更多