使用jQuery $就调用PHP函数 [英] using jquery $.ajax to call a PHP function

查看:143
本文介绍了使用jQuery $就调用PHP函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个简单的答案,但我使用jQuery的$就调用PHP脚本。我想要做的基本上是把那PHP脚本函数内调用从JavaScript的PHP函数。

This may be a simple answer, but I'm using jquery's $.ajax to call a PHP script. What i want to do is basically put that PHP script inside a function and call the PHP function from javascript.

<?php 
if(isset($_POST['something'] {
    //do something
}
?>

<?php
function test() {
    if(isset($_POST['something'] {
         //do something. 
    }
}
?>

我如何将调用的JavaScript函数?现在,我只是用$就与列出的PHP文件。

How would i call that function in javascript? Right now i'm just using $.ajax with the PHP file listed.

推荐答案

使用 $。阿贾克斯来调用服务器环境(或URL,或其他)来调用特定的'行动'。你需要的是这样的:

Use $.ajax to call a server context (or URL, or whatever) to invoke a particular 'action'. What you want is something like:

$.ajax({ url: '/my/site',
         data: {action: 'test'},
         type: 'post',
         success: function(output) {
                      alert(output);
                  }
});

在服务器端,动作 POST参数应阅读和相应的值应该指向方法来调用,例如:

On the server side, the action POST parameter should be read and the corresponding value should point to the method to invoke, e.g.:

if(isset($_POST['action']) && !empty($_POST['action'])) {
    $action = $_POST['action'];
    switch($action) {
        case 'test' : test();break;
        case 'blah' : blah();break;
        // ...etc...
    }
}

我相信这就是 Command模式简单的化身。

这篇关于使用jQuery $就调用PHP函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆