Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.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 &引用;xmlResult.Result未定义;错误_Php_Javascript_Ajax_Jquery - Fatal编程技术网

Php &引用;xmlResult.Result未定义;错误

Php &引用;xmlResult.Result未定义;错误,php,javascript,ajax,jquery,Php,Javascript,Ajax,Jquery,我制作了一个简单的php脚本,使用ajax通过邮政编码获取城市名称 我的java代码是 <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> <script type="text/javascript" src="http://jquery-xml2json-plugin.googlecode.com/svn/trunk/jquery.xm

我制作了一个简单的php脚本,使用ajax通过邮政编码获取城市名称 我的java代码是

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="http://jquery-xml2json-plugin.googlecode.com/svn/trunk/jquery.xml2json.js"></script>
        <script type="text/javascript">
        $(document).ready(function(){
            var initialPostal;
            $('#postal').bind('keyup change',function(e) {
            if(($("#postal").val().length==3 || $("#postal").val().length==6 || $("#postal").val().length==7) && $("#postal").val()!=initialPostal)
                { 
                              initialPostal=$("#postal").val();
                        $.get('citylookup.php?postal='+$("#postal").val(), function(xml){
                        var xmlResult = $.xml2json(xml);
                            $("#city").val(xmlResult.Result.City);
                            alert(xmlResult.Result.City);
                        }); } });   });  </script>

有解决方案吗?

您的第一个问题是,
$City
变量没有在
foreach
块之前声明-因此您将在那里得到一个错误


注意: 未定义变量:第14行citylookup.php中的城市
尝试:



您的第二个问题是JavaScript中没有错误检查-即,如果您键入未知邮件,您将看到相同的错误-请考虑检查返回值,以确保返回的内容确实有价值。您实际上只是使用上面的PHP返回一个单词。。。非XML/JSON

Alert/console.log将
xmlResult
记录在。。。我很确定这将帮助您解决问题…”Alert/console.log在“此解决方案不起作用”之前的行中记录xmlResult如果不是为了提供解决方案,而是为了帮助您。。。。我建议看一个叫做…的工具,我刚刚在英国的邮政编码上玩过这个,它可以工作-但是如果我用空格输入我的邮政编码,查找仍然可以工作,但是雅虎返回的邮政编码与输入的邮政编码不匹配。。。也要小心…我需要发布一个印度邮政编码,如363641382721见citylookup.php在$url中我提到一个县“India”
<form id="form1" name="form1" method="post" action="">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td>Postal:</td>
      <td><input type="text" name="postal" id="postal" /></td>
    </tr>
    <tr>
      <td>City:</td>
      <td><input type="text" name="city" id="city" /></td>
    </tr>
  </table>
<?php
$url = 'http://where.yahooapis.com/geocode?q='.$_GET['postal'].'&country=india';
$xml = simplexml_load_file($url);

foreach($xml as $result)
{
    if($result->postal==$_GET['postal'])
    {
    $City = $result->city;
    }

}
echo $City;
?>
Error: xmlResult.Result is undefined
Source File: http://localhost/postalcode/citylookup.html
Line: 17
<br /> <b>Notice</b>:  
Undefined variable: City in <b>citylookup.php</b> on line <b>14</b><br />
<?php
$url = 'http://where.yahooapis.com/geocode?q='.$_GET['postal'].'&country=india';
$xml = simplexml_load_file($url);

$City = "";
foreach($xml as $result)
{
    if($result->postal==$_GET['postal'])
    {
    $City = $result->city;
    }

}
echo $City;
?>