dsx666666 2017-02-14 14:07
浏览 68
已采纳

使用AJAX onclick函数返回PHP Mysql查询

So, I've been looking for a variety of sources to answer my question the last few day and thus have found nothing that's worked for me. I'll preface this further by saying that in regards to PHP and Javascript I started learning them like a week ago. I also understand that there will likely be better ways to format/write the code I'm about to post so please bear with me! :)

Essentially, I am trying to use a page name play.php in combination with AJAX to echo MYSQL queries back onto the page inside certain page elements.

So the code for main.js which is linked directly to play.php. I've tried about three different way that I've seen in various answers and have not gotten the information I wanted. I either get no response or I get undefined in all of them.

    function selectChar(uname, cname) 
{   
    var data = {
        username : uname,
        charname : cname
    };
    $.ajax({
        data : data,
        type : 'Get',
        url : 'start.php',
        dataType:"json",
        success : function (result) { 
        var data_character = JSON.parse(result);
        var cnamediv = document.getElementById('charactername');
        cnamediv.innerHTML = "";
        cnamediv.innerHTML = data_character[0].name;
        }
    }); 
}

The one above I see most often and the one below I just found earlier today. I get undefined when I attempt to call the array.

function selectChar(uname, cname) 
{
    $.get("start.php?username="+uname+"&charname="+cname).done(function(data_character){
        var cnamediv = document.getElementById('charactername');
        cnamediv.innerHTML = "";
        cnamediv.innerHTML = data_character[0].name;
    });
}

and finally the PHP code that queries the database and echos the data back.

<?php
    $conn = new mysqli($hostname,$username,$dbpassword, $dbname);

    if(!$conn) {
        die('Could not connect: ' . mysql_error());
    }

    $username = $_GET['username'];
    $charname = $_GET['charname'];

    $sql = "SELECT `id`, `username` FROM `users` WHERE `username` ='$username'";
    $result = mysqli_query($conn,$sql);

    //Send the array back as a JSON object
    echo json_encode($result);
?>

I'm not looking for someone to do work for me but I do require some guidance here. What would be an appropriate way to make this work? Is my code terribly incorrect? Am I missing an aspect of this altogether? Please, I would really seriously appreciate any help someone could give me!

P.S. I did just get done reviewing several other similar questions none of which seemed to help. Either there was never a conclusive outcome as to what worked for them or the solution didn't work when I attempted it.

展开全部

  • 写回答

2条回答 默认 最新

  • drvlf9739 2017-02-14 14:11
    关注

    try this:
    php get post and return json_encode

        if(!$conn) {
            die('Could not connect: ' . mysql_error());
        }
    
        $username = $_POST['username'];
        $charname = $_POST['charname'];
    
        $sql = "SELECT `id`, `username` FROM `users` WHERE `username` ='$username'";
        $result = mysqli_query($conn,$sql);
        $rows = array();
        while($r = mysqli_fetch_assoc($result)) {
         $rows[] = $r;
         }
        //Send the array back as a JSON object
        echo json_encode($rows);
    ?>
    

    JS ajax response and request

     $.ajax({
            data : data,
            type : 'POST',
            url : 'start.php',
            dataType:"json",
            success : function (result) { 
            console.log(result);
            document.getElementById('charactername').innerHTML = result[0].username;
            }
        }); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 在ros2的iron版本进行编译时遇到如下问题
  • ¥18 vs用setup project打包项目实现安装完立即运行
  • ¥15 孟德尔随机化TwoSampleMR在线提取结局数据,遇到Error in check_reset(override_429)的问题
  • ¥15 ONNX转RKNN遇到问题
  • ¥60 以太网电缆未接通怎么处理
  • ¥15 关于超声图片进行放射组学的疑问
  • ¥20 已经有功率放大电路图,具体每个元器件的参数怎么算?
  • ¥15 用GIS怎么提取出一个城市的中心城区
  • ¥30 matlab代码调试
  • ¥15 使用ruoyi分离版,下载完成引成功第三方插件报错,报错,换了好多插件都是一样的。
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部