Member Avatar for randomkid73

I'm having an issue accessing values of an AJAX response from a PHP script. I'm using an alert() for debugging purposes to show the data and it shows "undefined" as things are done below. Here's what I have so far.

jQuery AJAX request:

$(".edit").click(function(event) {
                var id = $(this).parents("tr").attr("id");
                alert(id); //Shows the correct id that matches the DB entry
                $.ajax({
                    type: 'POST',
                    url: 'editevent.php',
                    data: "eventID=" + id,
                    success: function(data) {
                        alert(data); //currently shows [object Object]
                    },
                    dataType: 'json'
                });
            });

editevent.php:

<?php
include "classes/Mysql.php";
$mysql = New Mysql();
$result = $mysql->load_Single_Event($_POST['eventID']);
echo json_encode($result[0]);// If I don't do this, the JSON object returned is an array (enclosed in []) but the data below is the only element
?>

//Relevant Mysql class code:
private $conn;

    function __construct() {
        $this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or 
                      die('There was a problem connecting to the database.');
    }
    function load_Single_Event($eventID) {
        $query = "SELECT * FROM events_2 WHERE Event_ID=$eventID;";
        $stmt = $this->conn->query($query);
        $result = $stmt->fetch_all(MYSQLI_ASSOC);
        return $result;
    }

The JSON response appears to be correct, here's a sample:
{"Event_ID":"8","Type":"Meeting","Title":"Test","Description":"Test","Location":"Test","InCharge":"","Date":"2012-09-11","StartTime":"06:00:00","EndTime":"10:00:00","Dept_ID":"10"}

SO... that all being said, I'm trying to access the data from the response to populate form fields for editing the entry. If more info is needed, I'll provide what I can but I can't currently think of anything else. Thanks in advance!

EDIT: Forgot to mention- I also tried using jQuery $.post() but same results. Also- trying to access via data.Title and data.title (both undefined)

function fetch_all() return array.
Try this

$(".edit").click(function(event) {
                var id = $(this).parents("tr").attr("id");
                alert(id); //Shows the correct id that matches the DB entry
                $.ajax({
                    type: 'POST',
                    url: 'editevent.php',
                    data: "eventID=" + id,
                    dataType: 'json',
                    success: function(data) 
                    {

                            if(data.length > 0)
                            {
                                for(key in data)
                                {
                                    var tmp = data[key];
                                    console.log(tmp.title);
                                }
                            }
                            else
                            {
                                console.log("no result");
                            }

                    },
                });
            });

alert(data[0].Title);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.