0

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

1 Answer 1

2

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.

7
  • 1
    JavaScript <script> blocks are executed/interpreted in the order they're found. Within a block function declarations are "hoisted" and so can be called by code that appears earlier, but this "hoisting" behaviour seems to be limited to individual <script> blocks. So this question's problem can be fixed by combining everything into a single <script> block or by moving the function declarations up to be first. stackoverflow.com/a/4145064/615754 Commented Feb 13, 2012 at 5:18
  • thank u Marc B, I moved the script block up to be called first but now it is giving me the following error: Uncaught TypeError: Cannot read property 'firstChild' of null Commented Feb 13, 2012 at 5:46
  • 1
    You're doing DOM manipulations, before the entire page has been loaded. You'll have to delay such things until AFTER the dom has been fully parsed/loaded, e.g. with jquery you need a $(document).ready() block. Commented Feb 13, 2012 at 5:47
  • sorry Marc B, I am not familiar with jquery where or how should I use this line $(document).ready() Commented Feb 13, 2012 at 5:53
  • @Omran - if you move all of your JS code to just before the closing </body> tag it should work without needing jQuery's $(document).ready() or the non-jQuery equivalent window.onload. Commented Feb 13, 2012 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.