unable to get response from ajax call using javascript in wampserver 2

  ajax, javascript, php

index.php

    <!DOCTYPE html>
     <html>

<head>
</head>
<body>
<script>
function funct1(str) {
  var xhttp;
  if (str.length == 0) { 
    document.getElementById("2").innerHTML = "";
    return;
  }
  xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("2").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "xxxx.php?q="+str, true);
  xhttp.send();   
}

function funct2(y,q) {
  var xhttp;
  if (str.length == 0) { 
    document.getElementById("3").innerHTML = "";
    return;
  }
  xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("3").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "yyyyy.php?y="+y+"&q="+q, true);
  xhttp.send();   
}
</script>

<div id="1">
<button onclick="funct1(this.value)" value="xyx"></button>
</div>
<div id="2">
</div>
<div id="3">
</div>
</body>
</html>

xxxx.php

<?php 
$q=$_GET('q');
$y="hello";
?>
<button value="<?=$y ?>" onclick="funct2(this.value,<?=$q ?>)"></button>

yyyyy.php

<?php

echo $_GET('y');
echo $_GET('q');

?>

In the above code when i click on first button xyz it calls function xxxx.php and returns button hello and when i click on hello it is supposed to go to yyyyy.php and print y,q values. But it throws error

Uncaught Referenceerror: $xyz is not defined
at HTMLButtonElement .onclick (index.php 1)

Source: Ask Javascript Questions

LEAVE A COMMENT