douwen1915 2012-02-09 16:43
浏览 59

如何在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!

  • 写回答

4条回答 默认 最新

  • douluxia0606 2012-02-09 16:47
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!