从PHP代码函数调用javascript函数未定义

时间:2012-02-13 05:07:17

标签: php javascript anonymous-function

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

<?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>

非常感谢您的帮助

1 个答案:

答案 0 :(得分:2)

按照文件中的顺序执行/解释Javascript。当您的PHP代码输出<script>showKML()</script>代码块时,尚未遇到实际的function showKML(..) {...}定义,因此您会收到此错误。

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