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'.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题