weixin_33671935 2014-11-13 06:04 采纳率: 0%
浏览 41

AJAX不返回任何数据

still fairly new to AJAX. I feel I'm not grasping it completely. I want to submit partial data to the server, perform an SQL query and return the results. This is what I have so far:

jQuery

j$('select[name=agent_Name]').change(function(event) {
     event.preventDefault();
     var agentID = j$(this).val();
     post_data = {'agent_ID':agentID};
     console.log("About to post data to the server");
    j$.post('../include/booking_Modify.php', post_data, function(response){  
        if(response.type == 'AgDEpCd'){
            output = response.text;
            console.log(output);
        }
        if(response.type == 'error'){
            output = response.text;
            console.log(output);
        }
    }, 'json');     
});

PHP

<?php
session_start();
require("../include/conn.php");
dbopen();
//check $_POST vars are set, exit if any missing
    if(!isset($_POST["agent_ID"]))
    {
        $output = json_encode(array('type'=>'error', 'text' => 'Nothing was selected!'));
        die($output);
    }

    $stmt = $conn->prepare("SELECT AgDEpCd FROM AGENTS WHERE AgentID = ?");
    $stmt->bind_param('i', $_POST["agent_ID"]);   // bind variables to the parameter
    $stmt->execute();

    $row = $result->fetch_assoc();
    $AgDEpCd = $row['AgDEpCd'];
    $stmt->close();
    $output = json_encode(array('type'=>'AgDEpCd', 'text' => $AgDEpCd));
    die($output);
?>

I checked to make sure: the file path was correct. var agentID = j$(this).val(); actually grabs a value, which it does Manually entered the SQL query into PHPMyAdmin to ensure I was retrieving results. I can't seem to return anything from the server. I'm not sure this is even possible. Please help!

  • 写回答

3条回答 默认 最新

  • weixin_33696822 2014-11-13 06:15
    关注

    Normally I will do just echo and exit, short and faster. In beforehand entering response, just console.log and check out if it return any. If it doesn't just check your php code, there is other error than the encoding output. Try it.

     <?php
        session_start();
        require("../include/conn.php");
        dbopen();
        //check $_POST vars are set, exit if any missing
            if(!isset($_POST["agent_ID"]))
            {
                echo json_encode(array('type'=>'error', 'text' => 'Nothing was selected!'));
                exit;
            }
    
            $stmt = $conn->prepare("SELECT AgDEpCd FROM AGENTS WHERE AgentID = ?");
            $stmt->bind_param('i', $_POST["agent_ID"]);   // bind variables to the parameter
            $stmt->execute();
    
            $row = $result->fetch_assoc();
            $AgDEpCd = $row['AgDEpCd'];
            $stmt->close();
            echo json_encode(array('type'=>'AgDEpCd', 'text' => $AgDEpCd));
            exit;
        ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵