Php 用Ajax选择不同的数据

Php 用Ajax选择不同的数据,php,javascript,jquery,ajax,Php,Javascript,Jquery,Ajax,我试图从insert.php页面中选择不同的数据位。如帖子id、用户名和用户id等。。我将添加所选数据的其他位。现在我可以用response获取div的id,但是当我添加更多的查询并将它们回显出来时,它们最终都在一起了,所以我将postdUserName都放在了一个字符串中。我怎样把它们分开 另外,我也知道MYSQL的贬值,我只是还没有时间更新我的代码 阿贾克斯 这是一个成功的例子:functionresponse、ResponseTo{等等。在PHP中,尝试以JSON格式返回数据 echo j

我试图从insert.php页面中选择不同的数据位。如帖子id、用户名和用户id等。。我将添加所选数据的其他位。现在我可以用response获取div的id,但是当我添加更多的查询并将它们回显出来时,它们最终都在一起了,所以我将postdUserName都放在了一个字符串中。我怎样把它们分开

另外,我也知道MYSQL的贬值,我只是还没有时间更新我的代码

阿贾克斯


这是一个成功的例子:functionresponse、ResponseTo{等等。

在PHP中,尝试以JSON格式返回数据

echo json_encode(array);
JavaScript中的响应变量将是一个可以从中获取数据的对象

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>");
            }
        });
    });
});

我冒昧地猜测了您希望在哪里使用“用户名”、“id”和“streamitem\u id”。

在PHP文件中,创建一个数组,并将要返回的数据添加到其中,即$json['streamitem\u id']=$Resultar['streamitem\u id']。一旦您想要返回的所有数据都是$json格式,echo json_encode$json。这更像是我所说的控制台日志。但这也很有用。在JavaScript中,假设jQuery能够确定响应是json格式的,则响应变量将是一个对象。如果您想确定,甚至可以执行typeofresponse。请参阅如何访问回复对象中的不同值:$homestatusid.prepend+newmsg+仍然困扰着我。有什么地方可以让我读得更清楚吗请Micah。这可能很简单,只是我似乎不明白。但我会继续为你的时间和耐心投票。再次感谢你这么多评论,我会投票,因为我无法投票给你编辑
console.log(response['username']);
$(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>");
            }
        });
    });
});
$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);