如何在Jquery请求期间从php运行Javascript函数?

I need a way to call a Javascript function during Jquery request from php.

All resquest of my site is made from Jquery by functions:Post(), get() and getJson(), but when try call a javascript from php nothing happen. :(

If I use submit of the html the javascript run well.


Then I would like to know what I need to do for the javascript work in this case?

Example I

My php code in index.php:

echo "<script>Message();</script>";

My JavaScript:

function Message()
{
    alert('JavaScript Executing!');
}

My Jquery code:

function Save()
{
    $.post
    (
        'index.php',
        {
            Action: 'Save'
        }
    )
}

Html Button:

<form>
<br><input type="button" value="Massage with Jquery!" onclick="Save()"><br>
<br><input type="submit" value="Message without Jquery!"><br>
</form>

I want click in the button "Message with Jquery" then to my php call the function javascript "Message".

Justifying the need: I could call this JavaScript directly in jQuery callback. The problem is that who knows when to run this JavaScript, Message(), is the client. :? And that jQuery, POST, has several clients. :? My views are reusable and only index.php know which javascript function should be executed or not.

Example II

Other example where the problem apper:

New.tpl

<script src="js/jquery.js" type="text/javascript"></script>
 <script>
function Save()
{
    $.post
    (
        'index.php',
        {
            Action: 'Save'
        }
    )
}
function Message()
{
    alert('JavaScript Executing!');
}
</script>
<form>
<br><input type="button" value="Massage with Jquery!" onclick="Save()"><br>
<br><input type="submit" value="Message without Jquery!"><br>
</form>

index.php

<?php
if( !isset($_POST['Action']))
{
        require_once($_SERVER['DOCUMENT_ROOT']."MeuTreinamentoPHP/smarty/templates/Controler/MySmarty.php");
        $MySmarty=MySmarty::getInstance();
        $MySmarty->Open('new.tpl');
}
echo "<script>Message();</script>";//It is essential that the JavaScript is called in index in this case. :?
?>

Thanks for all!

You have a misunderstanding here.

JQuery is a Javascript library. Both run on the client (browser).

PHP runs on the web server. Hence does not run Javascript.

You can get Javascript to request JSON etc from the web server - that may deliver the data using PHP

You can echo the JS

<?php
echo '<script>alert("JS in PHP.");</script>';
?>

Do you mean? This code is not tested

mainphp.php (or whatever the file name is)

<script>
    function save(){

    //Lets you know you will be executing
    alert('JavaScript Executing!');

    //Get Value
    var postVal = $("#ButtonValue").val();

    //Post the inputs data to the index file, to save maybe?
    //Then post back what you saved?
    $.post("index.php", { "postVal": postVal },
       function(data) {
         alert(data);
       });

    }
</script>

    //You Have this form, I added an ID

<?php
echo '
    <form>
    <br><input id="ButtonValue" type="button" value="Massage with Jquery!" onclick="Save()"><br>
    <br><input type="submit" value="Message without Jquery!"><br>
    </form>';
?>


Index.php will have

<?php

$postVal = $_POST['postVal'];
echo $postVal;

?>



This will alert the message "Massage with Jquery!" after you post it

So you want to tell the javascript what to do after it does the ajaxy magic.

$.post("index.php", 
    {Action: 'Save'}, 
    function(data) { eval(data); }
) 

this will effectively execute whatever javascript you return from server in your php don't send script tag but simply the code you want to execute:

echo "Message();"

Simpler and better solution will be to return command codes from server and act depending on them in client. this way you'll have all your javascript where it's supposed to be.