Shoutbox - AJAX + PHP =删除项目

时间:2015-03-07 22:56:08

标签: php ajax

我的页面上有简单的shoutbox,但我对删除喊叫有问题。

get.php

$data = array();

$q = mysql_query("SELECT * FROM shouts ORDER BY id DESC");

while ($row = mysql_fetch_assoc($q)) {
    $row .= '<a rel="'.$row['id'].'" class="delete_shout">[delete]</a> ID: '.$row['id'].' | '.$row['msg'].'<br />';
}

$data['shouts'] = $shouts;

echo json_encode($data);

add.php

$data = array();
$msg = $_POST['msg'];

mysql_query("
    INSERT INTO shouts
    VALUES (
        null,
        '".mysql_real_escape_string($msg)."',
        now()
    )
");

$data['msg'] = $msg;

echo json_encode($data);

我的JS:

$(document).ready(function(){

    get_shouts();

    $("#chat_send").submit(function(){

        var msg = $('input[name="msg"]').val();

        chat_send(chat_shout);

        return false;

    });

    $('a.delete_shout').live("click",function(){
        alert(1);
    });

    setInterval(function(){
        chat_refresh();
    }, 3000);

});

JS函数:

function chat_send(chat_shout) {

    if (chat_shout.trim() != '') {
        $.ajax({
            type: "POST",
            url: "./send.php",
            data: "chat_shout="+chat_shout,
            success: function(data) {
                get_shouts();
            }
        });
        $('input[name="msg"]').val('');
    }

}

function get_shouts() {

    $.ajax({
        type: "POST",
        dataType: "json",
        url: "./get.php",
        success: function(data) {
            $('.shouts').html(data["shouts"]);
        }
    });

}

function chat_refresh(last_shout_id) {
    get_shouts();
}

一切都很好,它的工作,但如何添加删除喊声选项?我添加了get.php:

<a rel="'.$row['id'].'" class="delete_shout">[delete]</a>

但如果我点击&#34;删除&#34;它不起作用......不是行动。

请帮助

0 个答案:

没有答案