php - Auto fill city after typing in zip code -
php - Auto fill city after typing in zip code -
i need auto fill fill city: form when typing in zip code, says "undefiened variable: array on line.." value="< ? php $array['navn'] ">
can help ?
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>"; } ?>
you'd want initialize variables. happens you're trying access variable hasn't been initialized yet.
<?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 request made, assign $city = $array['navn']; } ?> <!-- when echo, you'll never worry 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>
in answer, happens that, upon first load (no json request yet), values empty declared on top.
when submit form, variable assignment happens , substitutes values container.
simple demo
php json autofill zipcode city
Comments
Post a Comment