在输入邮政编码时,我需要自动填写填写CITY:表格, 它说“未定义的变量:数组在线...”,其中value =“<?php $ array ['navn']”>
有人可以帮忙吗?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>";
}
?>
答案 0 :(得分: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请求),值为空,但它们被声明在顶部。
当您提交表单时,会发生该变量赋值并将值替换为该容器。