0

I the following code:

<? $test = new com("Soundclass.Soundrec"); ?>
<? $test->startrec ?> 
<script>
function stop(){
    var stop_record = "<?=$test->stoprec;?>";
}
</script>

and I am running the stop function in a button click. But the php function seems to run without the click.

The purpose of this is to stop the recording on that button click.

3
  • How does your button look like? Commented Nov 23, 2011 at 9:37
  • Welcome to StackOverflow! Please accept the correct answer if there is any! Commented Nov 25, 2011 at 22:29
  • hey guys the code above doesnt work @Niels its a link not a button its someything like <a href='#' id='next'></a>, Commented Nov 28, 2011 at 22:34

3 Answers 3

3

You have a little logical error:

PHP is server-side and gets executed on the server when the document is called. Javascript is client-side and gets called dynamically, it can't re-execute any php-code again.

See the source code in your browser and you'll understand!

But you could make an ajax call to the class and execute the stoprec() method with a (defined in your script) get/post variable. But as this still won't give you a handle on the same instance of the object, so unfortunately you probably have to rethink your whole script!

Sign up to request clarification or add additional context in comments.

Comments

1

What you are trying to do is fundamentally impossible: You are mixing up PHP (which runs on server side) and JavaScript (which runs on client side).

You would have to build a second PHP script that stops the recording, and have that called from JavaScript using Ajax.

5 Comments

And sharing the same COM instance between multiple PHP scripts.
Guys do you have any examples that I can look into please?
Hey Guys since I have been told that I should use ajax on this I have made my way to that but hey still nothing seemed to happen I will be posting both files please advise on this solution please...here is my php file, that I would like to execute on the link click, this code is located in a folder public/test.php <?php function record(){ $test = new com("Soundclass.Soundrec"); $test->startrec = FALSE; $test->stoprec = FALSE; } function stop(){ $test->stoprec = TRUE; } ?>
and here is my html link on the views with the ajax call>: <script type="text/javascript"> $('#next').click(function(){ $.ajax({ type: 'POST', url: '/public/test.php', data: {'id':'45'} } }); }); </script> <a id="next" name="stop" href="#" onclick="validateAndSubmit()" style="text-decoration:none;color:blue></a> This code doesnt seem to work any mistakes I have made pls point them out to me please
@pumba: COM does not work with AJAX (short version). See stackoverflow.com/a/8239926/367456 and stackoverflow.com/a/8239970/367456
1

Calling $test->stoprec will probably stop recording (don't know Soundclass.Soundrec that specifically myself).

As you call it even before the page has been delivered, pressing the stop button later won't make a change here.

You need to execute the PHP later (after button click), however you have the general problem here that the com object in $test won't survive that (it would be a new one).

The only solution I see here is that you create a daemon that manages sound recording for you. This would work with AJAX, but is not trivial. So the short (and sad) answer is: Not easily possible.

2 Comments

I really dont understand what you are trying to tell me by this can you please put maybe some code examples or something, and please how do you do thi ?You need to execute the PHP later (after button click), as I have also tried to check the click with php 'isset' but still doesnt wanna work, I really dnt understand the issue with this com object. please help @hakre
I can not provide code examples for things that aren't possible, sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.