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

    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献