It been tricky question. PHP tag in the script tag does make to load the page and also unable to send the http request. When I remove or comment PHP tag from the script tag the page load and http request works fine as previously it was working.
<?php
function getFile(){
$file = "Table.txt";
$result = array();
ini_set('auto_detect_line_endings', true);
$fh = fopen($file, 'r');
$i = 0;
while (($line = fgetcsv($fh, 100000000, "\t")) !== false) {
$result[] = $line;
$i++;
}
}
?>
<html></html>
<script>
const urlParams = new URLSearchParams(window.location.search);
const p = urlParams.get("input");
const q = urlParams.get("query");
let xhr = new XMLHttpRequest();
xhr.open('GET', `http://127.0.0.1:8000/GetData?query=${q}&input=${p}`);
xhr.send();
// 4. This will be called after the response is received
xhr.onload = function () {
if (xhr.status != 200) { // analyze HTTP status of the response
alert(`Error ${xhr.status}: ${xhr.statusText}`); // e.g. 404: Not Found
} else {
**PHP call**
<?php getFile(); ?>
$('.mainRow').show();
**PHP call**
var result = <?php echo json_encode($result);?>;
var html = "<table id = both_table class = snpTbl>";
result[0].forEach(function (key) {
let newVal = key.replace(/_/g, " ").toUpperCase();
html += "<th>" + newVal + "</th>";
});
</script>
As you can see there is two PHP tag in script because of PHP call page unable to load and also HTTP request is unable to send.
When page load PHP code gets hidden because PHP is server side scripting but here it is not happening
Is I am writing code in wrong way???
Where I am Wrong Can anyone tell me Please. It will be very helpful to me.
Thank you in advance