weixin_33697898 2017-09-12 22:43 采纳率: 0%
浏览 23

PHP / AJAX从Ajax获取ID

First of all sorry for the English:) so hello im a new web programmer and im working on some school manager project.

i have to display all courses in school and then when i click course display his description.

this is my display php code

    <?php
$conn = mysqli_connect("localhost", "root", "", "school");

        if($conn){      
        $result = $conn->query("SELECT * FROM `courses`");
        if ($result->num_rows > 0){
            while($row = $result->fetch_assoc()){
                $id=$row['id'];
                echo '<div class="alex" onclick="myFunction(this.id)" id="'.$id.'">'.$row['name'].'</div>'.'<br>'.'<br>';
            }
        } 
        }else{
            echo die("connection faild"); 
        }  

        mysqli_close($conn);        
?>

as you can see i gave it the id of the table which i what to display description.

that is my function code in js file

     function myFunction(id){
        $.post(
            "api/courses/description.php",
            {  id:id }
        ).done(function( data ) {
            console.log(id);
            $('#desc').load("api/courses/description.php");
        });
    }

and here is my description php to get id and display description:

<?php $conn = mysqli_connect("localhost", "root", "", "school");
if (isset($_POST['id'])) {
$id = $_POST['id'];
}
if($conn){ $result = $conn->query("SELECT * FROM courses WHERE id = '$id'");
    if ($result->num_rows > 0){
        while($row = $result->fetch_assoc()){
            echo $row['description'];
        }
    } 
    }else{
        echo die("connection faild"); 
    }     
    mysqli_close($conn); ?>

when i click the displayed courses i get the ids on console! For every course I click I get his id on console.

hope you could help me! Im sittin on it for many hours and cant get the problem.

hope its readable T_T

edit:forgot to say.. error i have is

Notice: Undefined index: id in C:\xampp\htdocs\project\api\courses\description.php on line 9---cant get id NULL ----------------------------------------> on var dump

Notice: Undefined variable: id in C:\xampp\htdocs\project\api\courses\description.php on line 10 (---->id = $id undefined -> $id)

  • 写回答

1条回答 默认 最新

  • weixin_33696106 2017-09-12 23:06
    关注

    You're calling description.php twice. First you call it in an AJAX POST request and successfully supply it the id parameter. This does not generate an error. But then look at what you do with the result of that operation:

    console.log(id);
    $('#desc').load("api/courses/description.php");
    

    You log the id, which is also successful. Then you ignore the data response and call description.php again. This time as an AJAX GET request where you do not supply it with the id parameter. This is what generates the errors.

    Instead of making the second request, just use the returned value of the first request. It looks like you want something like this:

    console.log(id);
    $('#desc').html(data);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测