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

CBusBus
  • 2,321
  • 1
  • 18
  • 26
Omran
  • 551
  • 4
  • 15
  • 25

1 Answers1

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 1
    JavaScript ` – nnnnnn Feb 13 '12 at 05: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** – Omran Feb 13 '12 at 05: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. – Marc B Feb 13 '12 at 05:47
  • sorry Marc B, I am not familiar with jquery where or how should I use this line $(document).ready() – Omran Feb 13 '12 at 05:53
  • @Omran - if you move all of your JS code to just before the closing `

    ` tag it should work without needing jQuery's `$(document).ready()` or the non-jQuery equivalent `window.onload`.

    – nnnnnn Feb 13 '12 at 06:01
  • nnnnnn I have moved my js code before body end tag but it did not solve the problem (same problem **showKML is not defined.**) – Omran Feb 13 '12 at 06:08
  • Naveen Kumar, if i put the js code at the top of the page i will not be able to use getElementById. it will give this error Uncaught TypeError: **Cannot read property 'firstChild' of null** on this line 'map = new GMap2(document.getElementById("map"));' – Omran Feb 13 '12 at 08:49