2012-02-13 133 views
0

我有以下代码段从PHP代码调用showKML javascript函数。但是,它给了我以下错误:未捕获ReferenceError:showKML未定义从php代码函数调用javascript函数没有定义

<?php 
    $upload = $_SERVER['PHP_SELF']; 
    if(isset($_POST['kmltest'])) { 
     $target_path = "uploads/"; 
     $fn = basename($_FILES['uploadedfile']['name']); 

     $target_path = $target_path . basename($_FILES['uploadedfile']['name']); 
     //echo $target_path ; 
     if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
      echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded"; 
      echo "<script type=\"text/javascript\"> showKML(); </script>"; 
     }else 
      echo "There was an error uploading the file, please try again!"; 

    } 
?> 
<script type="text/javascript"> 
    function initialize() { 
     if (GBrowserIsCompatible()) { 
      map = new GMap2(document.getElementById("map")); 
      map.setCenter(new GLatLng(25.22903, 55.46612), 13); 
      map.addControl(new GSmallMapControl()); 
      map.addControl(new GMapTypeControl()); 
      map.clearOverlays(); 
      document.getElementById("lat").value = "25.22903"; 
      document.getElementById("lng").value = "55.46612"; 
     } 
    } 

    function showKML() { 
    //alert(filename); 
     initialize(); 
     document.getElementById('lat').disabled = true; 
     document.getElementById('lng').disabled = true; 
     var exml; 
     exml = new EGeoXml("exml", map, ("uploads/test.kml")); 
     exml.parse(); 
     exml.show(); 
    } 

    function startShape() { 
     ... 
    } 

    function startDrawing(poly, name, onUpdate) { 
      ... 
    } 

    function showcoor (poly) { 
     ... 
    } 

    function drawpoint() { 
     ... 
    } 

    </script> 

你的帮助是非常赞赏

回答

2

JavaScript是执行/解释在它在你的文件中找到的顺序。当您的PHP代码输出<script>showKML()</script>代码块时,实际的function showKML(..) {...}定义尚未遇到,所以您会收到此错误。

在运行PHP的东西之前移动要输出的函数定义。

+1

JavaScript'