blackpla9ue's solution to "HTML form that populates the city and state given the zip code"

You will need an external PHP file to read the web service and return the output to you as the URL fails the Same origin policy<\/a><\/p>\n\n

main file<\/strong> - check this fiddle http://jsfiddle.net/blackpla9ue/jy9TT/1/<\/a><\/p>\n

<script<\/span> type=<\/span>&quot;text/javascript&quot;<\/span>><\/span>\n$(<\/span>&quot;input<\/span>[<\/span>name=<\/span>'zip'<\/span>]<\/span>&quot;<\/span>).<\/span>bind<\/span>(<\/span>'change keyup'<\/span>,<\/span> function<\/span>()<\/span>{<\/span>\n\n    var<\/span> zipcode<\/span> =<\/span> $(<\/span>this<\/span>).<\/span>val<\/span>()<\/span>;<\/span>\n    var<\/span> isValidZip<\/span> =<\/span> /<\/span>(<\/span>^\\d{<\/span>5<\/span>}<\/span>$)<\/span>|<\/span>(<\/span>^\\d{<\/span>5<\/span>}-\\d{<\/span>4<\/span>}<\/span>$)<\/span>/.test<\/span>(<\/span>zipcode<\/span>)<\/span>;<\/span>\n    var<\/span> webservice<\/span> =<\/span> &quot;http://www.webservicex.net/uszip.asmx/GetInfoByZIP&quot;;<\/span>\n\n    if<\/span>(<\/span>isValidZip<\/span>)<\/span>{<\/span>\n        $<\/span>loader<\/span>.<\/span>show<\/span>()<\/span>;<\/span>\n        $.<\/span>ajax<\/span>(<\/span>{<\/span>\n            url:<\/span> 'proxy.php?url='<\/span> +<\/span> webservice<\/span> +<\/span> '?USZip='<\/span> +<\/span> zipcode<\/span>,<\/span>\n            datatype:<\/span> 'xml'<\/span>,<\/span>\n            success:<\/span> function<\/span>(<\/span>data<\/span>)<\/span>{<\/span>\n                $<\/span>loader<\/span>.<\/span>hide<\/span>()<\/span>;<\/span>\n\n                if<\/span>($(<\/span>data<\/span>).<\/span>find<\/span>(<\/span>'CITY'<\/span>).<\/span>length<\/span>)<\/span>{<\/span>\n                    $(<\/span>&quot;input<\/span>[<\/span>name=<\/span>'city'<\/span>]<\/span>&quot;<\/span>).<\/span>val<\/span>($(<\/span>data<\/span>).<\/span>find<\/span>(<\/span>'CITY'<\/span>).<\/span>text<\/span>())<\/span>;<\/span>\n                    $(<\/span>&quot;input<\/span>[<\/span>name=<\/span>'state'<\/span>]<\/span>&quot;<\/span>).<\/span>val<\/span>($(<\/span>data<\/span>).<\/span>find<\/span>(<\/span>'STATE'<\/span>).<\/span>text<\/span>())<\/span>;<\/span>\n                }<\/span>\n            }<\/span>                \n        }<\/span>)<\/span>;<\/span>            \n    }<\/span>\n\n}<\/span>)<\/span>;\n</script><\/span>\n\n<ul><\/span>\n    <li><\/span>\n        <label><\/span>Zip Code : </label><\/span>\n        <input<\/span> type=<\/span>&quot;text&quot;<\/span> name=<\/span>&quot;zip&quot;<\/span> /><\/span>\n        <span<\/span> class=<\/span>&quot;loading&quot;<\/span>><\/span>loading..</span><\/span>\n    </li><\/span>\n    <li><\/span>\n        <label><\/span>City: </label><\/span>\n        <input<\/span> type=<\/span>&quot;text&quot;<\/span> name=<\/span>&quot;city&quot;<\/span> /><\/span>\n    </li><\/span>\n    <li><\/span>\n        <label><\/span>State : </label><\/span>\n        <input<\/span> type=<\/span>&quot;text&quot;<\/span> name=<\/span>&quot;state&quot;<\/span> /><\/span>\n    </li><\/span>\n</ul><\/span>\n<\/pre><\/div>\n

proxy.php<\/strong><\/p>\n\n

<?php echo @file_get_contents($_GET['url']);?><\/code><\/p>\n

YouThe fiddle alone will not work because you need an external PHP file to read the web service and return the output to you as the URL fails the [Same origin policy](http://en.wikipedia.org/wiki/Same_origin_policy) **) **main file** - check this fiddle [http://jsfiddle.net/blackpla9ue/jy9TT/1/](http://jsfiddle.net/blackpla9ue/jy9TT/1/)
  • loading..
  • > **> **proxy.php** ``

User: blackpla9ue

Question: HTML form that populates the city and state given the zip code

Back to question