0

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

  • You also have a scope issue (if you didnt have a bigger issue with where php and javascript runs) – RiggsFolly Apr 21 '21 at 10:39
  • And its a bit of a mystery where `$result_2` might be coming from – RiggsFolly Apr 21 '21 at 10:41
  • @RiggsFolly It is also coming from PHP which I have edited. Sorry for that. Now I have edited the question. – aryan vishwakarma Apr 21 '21 at 10:44
  • @RiggsFolly how Scope issue Sir? Can you please provide me more detail on it – aryan vishwakarma Apr 21 '21 at 10:46
  • Well its no really relevant until you sort out the _difference between client-side and server-side programming_ But you create `$result` inside a function, it is therefore invisible to code that is not also inside that function. It is destroyed as soon as the function finishes, and as you dont `return` it, to all intents and purpose does not exist – RiggsFolly Apr 21 '21 at 10:48
  • But this information wont fix the basic issue in this code – RiggsFolly Apr 21 '21 at 10:49
  • @RiggsFolly Okay Sir. Got your point – aryan vishwakarma Apr 21 '21 at 10:53
  • your problem no/1 is damn **PHP call** its self. valid Js comment either start with // or */ for multi line. You have placed this inside JS script tag php does not give a dman about it and does not even bother other then printing it out as it is. BUT Js when comes to this line comes to halt throwing error or exception. check browser console error would be there ...Uncaught SyntaxError: Unexpected token '**' – Syed Apr 21 '21 at 11:03
  • **PHP call** is not even a php comment code as far as I know ...Parse error: syntax error, unexpected '**' (T_POW) but since you had placed it out side of php tags php didn't bother but JS did – Syed Apr 21 '21 at 11:06
  • Rest is scope issue f you just place return $result; on get file function and then make should solve your issue of scope and rest of logic – Syed Apr 21 '21 at 11:13
  • @Syed Sorry Sir for Late Reply. I didn't get any error instead page is loading continuously. – aryan vishwakarma Apr 22 '21 at 05:46
  • @aryan vishwakarma I hope so it works .... if I place the code as it is with double Sterics seemed have blocked the JS execution and if moved to PHP threw tantrum as well. If sterics wont go into code on your end, then simple update code as dependency injector etc – Syed Apr 22 '21 at 13:14

0 Answers0