Akira Dawson
Akira Dawson

Reputation: 1267

AJAX isn't returning any data

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!

Upvotes: 0

Views: 76

Answers (3)

tharindu_DG
tharindu_DG

Reputation: 9261

Just echo the result set at the end of the php script. It will be assigned to the ajax response data.

$data = array('type'=>'AgDEpCd', 'text' => $AgDEpCd);
echo json_encode($data);

Upvotes: 0

Domain
Domain

Reputation: 11808

You are not assigned result to $result variable.

It should be,

$result = $stmt->execute();

Upvotes: 1

Eric T
Eric T

Reputation: 1026

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

Upvotes: 1

Related Questions