dongyi6845 2015-12-01 18:44
浏览 151
已采纳

如何在ajax中调用php类方法

I'm trying to retrive some code from a php method using ajax, when a person click a div.

HTML

<div class="contenitore">
    <div>
        <input class="idCommento" value="//some number">
    </div>
    <div>
        <p class="vediRisp">Create text</p>
    </div>
</div>
<div class="contenitore">
    <div>
        <input class="idCommento" value="//some number">
    </div>
    <div>
        <p class="vediRisp">Create text</p>
    </div>
</div>

PHP (lib/Users.class.php)

Class Users{
    public function dammi_i_commenti(arguments){
        return '<div>code</div>';
    }
}

(lib/ottieniCose.php)

require('Users.class.php');
$login = New Users;

$GLOBAL['current'] == $_GET['adesso'];
print $login->dammi_i_commenti();

AJAX

$(".vediRisp").on('click', function() {
    var mh = $(this).parentsUntil(".contenitore").find(".idCommento");
    var eeeh = mh.val();
    $.ajax({
        url: "lib/ottieniCose.php",
        type: "GET",
        data: {
            adesso: eeeh,
        },
    })
    .done(function() {
        console.log("success");
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("complete");
    });
});

When i click the .vediRisp in the console i can read success, then complete, so I'm thinking that the click and ajax function work, but the page doesn't seem to update its content therefore i think that i'm not calling the method correctly. Anyway if i try to write var_dump("stuff") or print_r("stuff") in the ottieniCose.php file i get absolultely no result at all, and my guess is that i'm not using the ajax function correctly, or the ottieniCose.php file isn't called. Any help?

  • 写回答

1条回答 默认 最新

  • dsfdsf8888 2015-12-01 19:30
    关注

    You need to add the response to the page using jquery.

    .done(function(response) {
            console.log("success");
            $("#<some div id>").html(response)
        })
    

    Simply printing it on the php script wont add it to he page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配