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!