1

I have a piece of Javascript that invokes a social publishing widget overlay. When you load the page the overlay will popup and this is the script that calls it:

<script type="text/javascript">
  RPXNOW.loadAndRun(['Social'], function () {
    var activity = new RPXNOW.Social.Activity(
       "Share your comment",
       "commented on 'Like My New Perfume?' on cuteoverload.com",
       "http://cuteoverload.com/2009/10/26/like-my-new-perfume/");
    RPXNOW.Social.publishActivity(activity);
  });
</script>

I need put somehow create a javascript or php function that will fire when this PHP function is called:

function response($oAlert) {
    // something to happen here;
}
1
  • 1
    just name the function, and then echo "<script>javascriptFunctionName()</script>" Not pretty but it will get the job done. Commented Aug 16, 2010 at 22:05

2 Answers 2

1

PHP is server side, so if you want JavaScript to interact with it you will have to push information to it via an HTTP request. I'd recommend building an AJAX request to a .php file specialized to dealing with different commands. IE:

switch ($_POST['command']) {
    case "post" : doPost(); break;
    case "dance" : doDance(); break;
    default : break;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I think that's the best way to go.
0

Since PHP runs on the server and Javascript runs on the client, you can call a remote PHP function from within a Javascript (using an XMLHTTP Request) but you cannot call a Javascript function from a PHP function. The above code snippet is doing a callback to the publishActivity function so you could call your PHP function inside the callback and you can trigger another responder javascript function which will wait for the AJAX response and trigger depending on what the response is.

2 Comments

acctually you can call javascript function from php. Byron Whitlock has written the way: echo "<script>javascriptFunctionName()</script>".
Thanks, I'll make a function to post some data using curl then display the js based on what was posted.

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.