选择数据Ajax

时间:2012-08-02 14:10:47

标签: php javascript jquery ajax

我想从insert.php中选择streamitem_id并将其添加到我现有的post div中。我需要发布的帖子的ID,所以我可以将它添加到我的div中,所以稍后当我添加我的删除按钮时,它将删除帖子和保存它的div。希望我有道理,我可以用我正在努力学习的东西来做到这一点,但是当它刚开始时它就像任何一种语言一样困难。

AJAX

 <script>
    $(document).ready(function(){
        $("form#myform").submit(function(event) {
            event.preventDefault();
            var content = $("#toid").val();
            var newmsg = $("#newmsg").val();

            $.ajax({
                type: "POST",
                url: "insert.php",
                data: "toid=" + content + "&newmsg=" + newmsg,
                success: function(){
                    $("#homestatusid").prepend("<div id='divider-"+WHERE MY STREAMITEM_ID NEEDS TO BE+"'><div class='userinfo'>"+newmsg+"</div></div>");
                }
            });
        });
    });
    </script>

INSERT.PHP

$check = "SELECT streamitem_id FROM streamdata WHERE streamitem_id=$user1_id";
        $check1     =    mysql_query($check);
        $check2     =    mysql_num_rows($check1);
    echo $check2;

3 个答案:

答案 0 :(得分:2)

在你的javascript中应该是

       $.ajax({
            type: "POST",
            url: "insert.php",
            data: {toid:content, newmsg: newmsg}, # pay attention to this line
            success: function(data){
                $("#homestatusid").prepend("<div id='divider-"+data+"'><div class='userinfo'>"+newmsg+"</div></div>");
            }
        });

PHP

$toid = isset($_POST['toid']) ? $_POST['toid'] : null;
$newmsg = isset($_POST['newmsg']) ? $_POST['newmsg'] : null;

不要使用mysql_*,因为它已弃用

答案 1 :(得分:1)

success: function(response){
    $("#homestatusid").prepend("<div id='divider-"+response+"'><div class='userinfo'>"+newmsg+"</div></div>");
}

答案 2 :(得分:1)

传递给success回调的第一个参数是来自AJAX调用的responseText。将您的jQuery代码修改为:

success: function(responseText){
    $("#homestatusid").prepend("<div id='divider-"+responseText+"'><div class='userinfo'>"+newmsg+"</div></div>");
    // I'm assuming that insert.php returns just the ID you're interested in
}