My code is doing the following (note all HTML, JavaScript and PHP code is on the same file):
A form is used for entering data and is validated by PHP. The PHP gets JSON data from a Google API and sends it to JavaScript . A JavaScript function parses the JSON and displays the required fields in a table.
I want to call the JavaScript function from PHP after getting the JSON but when I call it from PHP , console displays function not defined. The same code works when I call the function from an HTML button. I have tried defining the function before the PHP script, after it, in the head, basically everywhere. When I define it in the head with my other JavaScript functions the other functions also get function not defined error.
The JavaScript function:
    function generateTable() {
        var placejs = <?php echo json_encode($placesresponse); ?>; //getting the json from php
        var pjs = JSON.parse(placejs);
        var body = document.getElementsByTagName("body")[0];
        var tbl = document.createElement("table");
        var tblBody = document.createElement("tbody");
        var results = pjs.results;
        //Outputing the headers
        var row = document.createElement("tr");
        var thead = document.createElement("th");
        var headtext = document.createTextNode("Category");
        thead.appendChild(headtext);
        row.appendChild(thead);
        thead = document.createElement("th");
        headtext = document.createTextNode("Name");
        thead.appendChild(headtext);
        row.appendChild(thead);
        thead = document.createElement("th");
        headtext = document.createTextNode("Address");
        thead.appendChild(headtext);
        row.appendChild(thead);
        tblBody.appendChild(row);
        //outputting the values
        for (var i = 0; i < results.length ; i++) {
            row = document.createElement("tr");
            var curresult = results[i];
            var result_keys = Object.keys(curresult);
            for (var j = 0; j < result_keys.length ; j++) {
                var cell = document.createElement("td");
                var cur = result_keys[j];
                if(result_keys[j] == "icon")
                {
                var im = document.createElement("img");
                im.setAttribute("src",curresult[cur]);
                cell.appendChild(im);
                row.appendChild(cell);
                }
                if(result_keys[j] == "name")
                {
                var cellText = document.createTextNode(curresult[cur]);
                cell.appendChild(cellText);
                row.appendChild(cell);
                }
                if(result_keys[j] == "vicinity")
                {
                var cellText = document.createTextNode(curresult[cur]);
                cell.appendChild(cellText);
                row.appendChild(cell);
                }
            }
            tblBody.appendChild(row);
        }
        tbl.appendChild(tblBody);
        body.appendChild(tbl);
        tbl.setAttribute("border", "1");
        tbl.style.marginLeft = "auto";    
        tbl.style.marginRight = "auto"; 
        tbl.style.borderCollapse = "collapse";
    }
The part in PHP where I am calling this:
    $placesurl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$latitude,$longitude&radius=$radius$type&keyword=$keyword&key=$placeskey";
        $placesresponse = file_get_contents($placesurl); //getting the json
        $placesjson = json_decode($placesresponse,true);
        if($placesjson['status']=="ZERO_RESULTS"){
            echo '<script>';
            echo 'noresult();';
            echo '</script>';
        }
        else{
            echo '<script>';
            echo 'generateTable();'; //function call
            echo '</script>';
        }
This is an assignment and I have to follow guidelines so I cannot just generate the table from PHP. Also I cannot use jQuery for the same reason. Any help would be appreciated, I am new to this stuff.
edit:
1) Sorry for not posting this earlier, the console shows this one extra error: "SyntaxError: expected expression, got '<'" for the line where i assign the json to the javascript variable:
    var placejs = <?php echo json_encode($placesresponse); ?>;
2) I tested a little bit more and its clear that whatever function i define inside the same script tags as generateTable() is also shown as undefined in the console. And the script tag is placed before the function call.
 
     
     
    