Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 67441

How to properly display php file as javascript alert using XMLHttpRequest? [duplicate]

$
0
0

This question already has an answer here:

I am writing some JavaScript code so that when a user clicks a button, a list of something appears as a JavaScript alert.

I have written an AJAX request using XMLHttpRequest. However, when I press the button, undefined appears in the alert box. I expected the list from the php file to be displayed in the alert box. Below is the code I've written:

<!DOCTYPE html>
<head>
<body>


<h1>Avengers Characters Search</h1>

<button onclick = "search()"> Search </button>
<script type="text/javascript" src="app.js"></script>


</body>
</html>

JavaScript Code:

function search(showmethemoney) {
  alert(showmethemoney);
}

function showmethemoney() {
  var x = new XMLHttpRequest();
  x.responseType = 'php';
  x.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("search").onclick = this.responseText;
    }
  };

  x.open("GET", "superheroes", true);
  x.send();
}

Viewing all articles
Browse latest Browse all 67441

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>