简体   繁体   中英

calling javascript function from php code function is not defined

I have the following segment of code that calls showKML javascript function from php code. However, it gave me the following error: Uncaught ReferenceError: showKML is not defined .

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

your help is really appreciated

Javascript is executed/interpreted in the order it's found in your file. At the time your PHP code outputs the <script>showKML()</script> code block, the actual function showKML(..) {...} definition has not yet been encountered, so you get this error.

Move the function definition to be output BEFORE your run your PHP stuff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM