donglian6625 2017-02-22 12:57
浏览 33
已采纳

Ajax请求php文件输出mysql数据

I am trying to output the row in the database which matches the forename and surname of the student from the dropdown list. i've got the function storing the first name and surname in the variable clickeditem. I must be going wrong in the Ajax/php side of things, possibly the way in which accessing the variable? When I click a name in the dropdown list nothing is occurring at all. Also I am echoing out the 'result' in the php file, how do I change that so the ajax can deal with the 'success'? The code with html and ajax:

    <script 

        $.ajax({
                type: "POST",
                url: "out.php",             
                data: {item : item},              
                success: function(data){                    
                    $("#result").html(data); 
                    //alert(response);
                }

                });
    });
</script>

I have used explode to separate the first name and surname from the string as it obviously will be all in one.

  • 写回答

2条回答 默认 最新

  • douhan4243 2017-02-22 13:55
    关注

    You've set up a test which will cause the rest of your code to fail:

    $student = isset($_POST['clickeditem']);
    

    $student will be either 'true' or 'false', not the string containing the first and last name. At this point your code will fail. Change the line to this:

    $student = $_POST['clickeditem'];
    

    Now $student is set properly and the rest of your code should work as you expect.

    You also have an error in the query itself ( comma where an AND should go ):

    WHERE student_forename=:forename, student_surname=:surname');
    

    should be:

    WHERE student_forename=:forename AND student_surname=:surname');
    

    One last thing: you cannot echo $result; because it is an array in this section of your code:

    if($result['count'] == 1){
        echo $result;
    }
    

    You must either echo $result['count']; or echo $result[0]; or convert the array to another format which will be returned by your AJAX request.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(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】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部