0

I have been researching into using PHP and Javascript to communicate with each other on the same page.

Firstly I have a php script at the top with a function to retrieve a audio from an AWS Ivona media stream.

<?php
    function createAndPlay($text){
        // request service get response save to `$data`
        // I want this to then play the sound
    }
?>

<script>
// when button clicked createAndPlay("Text to Speak")
<script>

I would like this to be done on the same page WITHOUT starting when the body of the document is first loaded.

Shira
  • 6,392
  • 2
  • 25
  • 27
  • 6
    This needs to be done with ajax making request to server. Php only runs on server and your javascript only runs in the browser – charlietfl Oct 05 '16 at 14:23

1 Answers1

0

Based on your last sentence:

I would like this to be done on the same page WITHOUT starting when the body of the document is first loaded.

It sounds like your situation is that all of this is working, but that the audio auto-plays when you load the page. That makes sense considering you have your call in a <script> tag. Javascript will process it as soon as it sees it and play the audio. It is hard to say since you have not included that actual code.

You would need to move the script block to a link or button or something:

 <a href="#" onclick='createAndPlay("Text to Speak");'>Click to hear</a>
Digital Chris
  • 6,177
  • 1
  • 20
  • 29