AJAX get variable from another file and use as location for audio

Hello i use ajax code to open another file and in second file have php where connecting to database and retun value. Can use this value to play audio in first file and all only click one button ?
in first file index.php have:

<button class="button"  style= "border-radius: 12px;  name="users" onclick="play(this.value)" value="1">Play</button>


<script>
function play(str) {
  if (str == "") {
    document.getElementById("txtHint").innerHTML = "";
    return;
  } else {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        document.getElementById("txtHint").innerHTML = this.responseText; 
		document.getElementById("txtHint").play();		
      }
    };
    xmlhttp.open("GET","play.php?q="+str,true);
    xmlhttp.send();
	
	

  }
}
</script>

in second file play.php have :

$sql ="SELECT * FROM playlist ORDER BY point DESC LIMIT 1";
$result = $conn->query($sql);



	if ( !empty($result->num_rows) && $result->num_rows > 0) {
		while($row = $result->fetch_assoc()) {
			
				$conn->close();
				
				include 'config.php';
				$GLOBALS['p'] = $row["point"];
				$p = $GLOBALS['p'];
				$sql ="SELECT * FROM playlist WHERE (point = $p) ORDER BY RAND() LIMIT 1";
				$result = $conn->query($sql);
				while($row = $result->fetch_assoc()) {
				$name = str_replace("\"","'", $row["name"]);
				$GLOBALS['played'] =  $name;
				echo '<marquee behavior="scroll" direction="left" style= "color:white;"><b><a>'.$GLOBALS['played'].'</a></b></marquee> ';
				$play = "playlist1/".$name;
				$playnow = 0;
				$point = $row["point"] - (($row["playtimes"] + 1) * 10);
				$playtimes = $row["playtimes"] + 1;
				$id = $row["id"];
				

				
				echo $play;
				
				
				}
		/*<?php echo $row["id"] ?> */
	
}}

When use button ‘Play’ return $play from second file. This is my path ho want to use for element to play.

I guess I tackle my AJAX stuff differently? But you should be able to check/get a response and handle it however is needed. At what point is it failing? Are you not getting a response? Have you output the response?

Is the question how to programmatically .click() or .trigger() a button or something?

Sponsor our Newsletter | Privacy Policy | Terms of Service