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;
        ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥50 adb连接不到手机是怎么回事?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目