posting data ajax, php

时间:2016-05-11 11:33:45

标签: php jquery

I tried to make a comment box, in which every user can delete their own comments.

Everything is good, less delete button. I can comment but when i click x button(delete btn), there is not posted data before success.

jquery-code

$(document).ready(function(){

    $('.buton-stergere').each(function(){

        var buton = this;
        $(buton).click(function(){
            stergere_com(buton.id);

        });

    }); 

});

function stergere_com(_id_comment){

    $.post("/site/ajax/stergere_com.php", {
        task: "stergere_com",
        Serverside: true,
        id_comment: _id_comment
    }
    ).success(function(data)
    {
        console.log("succes");
    });

}

php code

if(isset($_POST['task']) && $_POST['task'] == 'stergere_com'){
    require_once $_SERVER['DOCUMENT_ROOT'].'/directoaredef.php';

    echo "server side";
}

html btn

 <ul>
    <li id="<?php echo $comment->id_comment; ?>" class="buton-stergere">X</li>

 </ul>

2 个答案:

答案 0 :(得分:0)

您不需要使用$ .each,只需使用按钮类进行点击活动。

    Desktop desktop = Desktop.getDesktop();
    File file = new File("/Applications/yourApp.app");
    try {
        desktop.open(file);
    } catch (IOException e) {
        e.printStackTrace();
    }

答案 1 :(得分:0)

尝试:

$(document).ready(function(){

    $('.buton-stergere').each(function(){

        var buton = $(this);
        buton.click(function(){
            stergere_com(buton.attr('id'));

        });

    }); 

});

function stergere_com(_id_comment){

    $.post("/site/ajax/stergere_com.php", {
        task: "stergere_com",
        Serverside: true,
        id_comment: _id_comment
    }
    ).success(function(data)
    {
        console.log("succes");
    });

}

如果你需要var buton作为Javascript这个 - 而不是jQuery对象$(this):

var buton = this;

使用:

var buton = $(this)[0];