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 如何用Thoony写ESP32温湿度检测无源蜂鸣器报警代码?
  • ¥20 部件内部的CT图像数据集
  • ¥15 Visual studio调用动态库
  • ¥15 双目摄像头标定后的校准文件
  • ¥15 powerbi矩阵增加度量值后出现对应关系错乱
  • ¥30 频率分析法分析绘制奈奎斯特图、波特图
  • ¥15 弹出来一万个系统找不到指定的文件框框,怎么解决
  • ¥15 ADS生成的微带线为什么是蓝色空心的
  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow