donglian6625 2017-02-22 20: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 21: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 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥100 有偿求易语言word文档取doc和docx页数方法或模块
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低
  • ¥15 目标计数模型训练过程中的问题
  • ¥100 Acess连接SQL 数据库后 不能用中文筛选
  • ¥15 用友U9Cloud的webapi
  • ¥20 电脑拓展屏桌面被莫名遮挡