dongshan8953
2012-08-02 14:58
浏览 12
已采纳

使用Ajax选择不同的数据

I'm trying to select various bits of data out of my insert.php page. Such as post id, username and users id so far.. And I will be adding other bits of selected data. Now I could get the id of the div with response, but when I added more queries and echo them out they ended up all together so I had my postidusernameuserid all in one string. How do I separate them?

Also I'm aware of the depreciated MYSQL_ I just haven't got around to updating my code yet.

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(response){
                    $("#homestatusid").prepend("<div id='divider-"+response+"'><div class='userinfo'>"+newmsg+"<a href='/profile.php?username="+response+"'><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border=\"0\" src=\"imgs/cropped"+response+".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" height=\"40\" ></a><div style='cursor:pointer;position:relative;top:0px;float:right;padding-right:5px;' onclick=\"delete_('"+response+"');\">X</div></div></div>");
                }
            });
        });
    });
    </script>

INSERT.PHP

$check = "SELECT streamitem_id FROM streamdata";
        $check1     =    mysql_query($check);
        $resultArr = mysql_fetch_array($check1);
    echo $resultArr['streamitem_id'];

$check = "SELECT username,id FROM users";
        $check1     =    mysql_query($check);
        $resultArr = mysql_fetch_array($check1);
    echo $resultArr['username'];
    echo $resultArr['id'];

Is it a case of success: function(response,responsetwo){ and so on.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dourangdz750379 2012-08-02 15:03
    已采纳

    In PHP try returning the data in JSON format.

    echo json_encode(array);
    

    The response variable in JavaScript will be an object you can get the data from.

    console.log(response['username']);
    

    JavaScript:

    $(document).ready(function(){
        $("form#myform").submit(function(event) {
            event.preventDefault();
            var content = $("#toid").val();
            var newmsg = $("#newmsg").val();
    
            $.ajax({
                type: "POST",
                url: "insert.php",
                dataType: "json",
                data: { toid: content, newmsg: newmsg },
                success: function(response){
                    $("#homestatusid").prepend("<div id='divider-"+response['streamitem_id']+"'><div class='userinfo'>"+newmsg+"<a href='/profile.php?username="+response['username']+"'><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border=\"0\" src=\"imgs/cropped"+response['id']+".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" height=\"40\" ></a><div style='cursor:pointer;position:relative;top:0px;float:right;padding-right:5px;' onclick=\"delete_('"+response['id']+"');\">X</div></div></div>");
                }
            });
        });
    });
    

    PHP:

    $json = array();
    $check = "SELECT `streamitem_id` FROM `streamdata`";
    $check1 = mysql_query($check);
    $resultArr = mysql_fetch_array($check1);
    $json['streamitem_id'] = $resultArr['streamitem_id'];
    mysql_free_result($check1);
    
    $check = "SELECT `username`, `id` FROM `users`";
    $check1 = mysql_query($check);
    $resultArr = mysql_fetch_array($check1);
    $json['username'] = $resultArr['username'];
    $json['id'] = $resultArr['id'];
    mysql_free_result($check1);
    
    echo json_encode($json);
    

    I took the liberty of guessing where you wanted to use 'username', 'id', and 'streamitem_id'.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题