0

I tried to use a simple echo function with JS in it to show alerts and prompts, in a php page which is called using AJAX. I looked at lot of other SO posts, but couldn't find any working solution, like this one. Here is my code:

//AJAX
function callPhp(opt, algorithm, arrayReq, solArrayReq) {
if (window.XMLHttpRequest) {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp = new XMLHttpRequest();
}
else {
  // code for IE6, IE5
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    document.getElementById("debug").innerHTML = this.responseText;
  }
};
xmlhttp.open("GET", "selectknown.php?q="+opt+"&alg="+algorithm, true);
xmlhttp.send();

And

//selectknown.php
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <?php
      echo '
        <script type="text/javascript">
          alert("Hello world! This is an Alert Box.");
          var accepted = prompt("enter the letters \'yes\' here");
        </script>
      ';
    ?>
  </body>
</html>
aravk33
  • 469
  • 2
  • 10
  • 18

2 Answers2

0

No need to echo the script out, just use it as html.

//selectknown.php
<!DOCTYPE html>
<html>
  <head>
  <meta charset="utf-8">
  <title></title>
</head>
<body>
    <script type="text/javascript">
      alert("Hello world! This is an Alert Box.")
    </script>
 </body>
</html>
aravk33
  • 469
  • 2
  • 10
  • 18
JParkinson1991
  • 1,256
  • 1
  • 7
  • 17
  • Actually, I can't do that that, because I'm executing this based on the value of a php variable. How will I be able to make it work in my case? – aravk33 Sep 29 '17 at 15:00
  • Also, I tried your code, but it didn't work. This problem seems to be impossible to solve, because I simply can't find out why this isn't working. – aravk33 Sep 29 '17 at 17:09
  • Can you not just call the alert from your current script tag? Rather than using the get call? – JParkinson1991 Sep 29 '17 at 17:10
  • No. I am executing a lot of code along with this, and this only gets executed if certain conditions are true. However, it isn't working even if this is the only code present. – aravk33 Sep 29 '17 at 17:46
0

Firstly, if you wish to put a script tag in an echo statement, it is possible. I think your problem is that you are running the code as a .html file instead of running it as a .php file. Just name the file something like file_loader.php The code needs to run on a server. PHP is a server side scripting language. Or you could copy and paste the code below into a php playground to quickly see it working.

<html>
<head>
<body>
<?php
  echo '
    <script type="text/javascript">
      alert("Hello world! This is an Alert Box.");
      var accepted = prompt("enter the letters \'yes\' here");
    </script>
 ';
?>    
<div id='debug'>Waiting for file to load.....</div>
<script>
//AJAX
 function callPhp(opt, algorithm, arrayReq, solArrayReq) {
   if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
     xmlhttp = new XMLHttpRequest();
   }
   else {
   // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   xmlhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
     document.getElementById("debug").innerHTML = this.responseText;
   }
};
xmlhttp.open("GET", "selectknown.php?q="+opt+"&alg="+algorithm, true);
xmlhttp.send();
</script>
</body>
</html>

Secondly, in order to load the file from the ajax code, you need to add an element that is identified by the specified Id - debug e.g.

<div id='debug'>Waiting for file to load.....</div>

Hope you find that helpful.

Temi Fakunle
  • 672
  • 6
  • 7
  • All my files except for the ones which have js or css are named with .php extensions. Also, will this code work if both the js and php are in the same file, as you have shown? – aravk33 Sep 29 '17 at 16:49
  • I tried your code. So what happens is, the code gets executed on page load, instead of when the function is called. – aravk33 Sep 29 '17 at 16:57
  • The function should not be executed unless its called. You can add a button to html to call the function e.g. : – Temi Fakunle Sep 29 '17 at 17:42
  • Yes, but how to stop the php from executing until then? – aravk33 Sep 29 '17 at 17:44
  • To stop php from automatically executing, you can put the php code within a function, and use an event (onclick, onblur etc) to call the function. – Temi Fakunle Sep 30 '17 at 11:58