AJAX data object returning undefined

Trying to prepopulate a certain input field in a form with latest data from the db, but the data returned is undefined. SQL query works perfectly. I’m using a similar script to generate the table. So no problem there.

the PHP code to generate json

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?php
include('db.php');
include('function.php');
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
$compressor_hours = '';
//get the last read runtime hours of each compressor
//return one row of plant_readings_id | runtime_hours1 | runtime_hours2 | runtime_hours3
$compressor_hours .= "SELECT MAX(r.id),
MAX(CASE c.compressor_name WHEN 'CM101' THEN c_r.runtime_hours END) AS runtime_hours1,
MAX(CASE c.compressor_name WHEN 'CM201' THEN c_r.runtime_hours END) AS runtime_hours2,
MAX(CASE c.compressor_name WHEN 'CM301' THEN c_r.runtime_hours END) AS runtime_hours3
FROM plant_readings r
LEFT JOIN compressor_readings c_r ON c_r.plant_readings_id = r.id
LEFT JOIN compressors c ON c_r.compressor_id = c.id;";
$statement = $connection->prepare($compressor_hours);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
$filtered_rows = $statement->rowCount();
foreach($result as $row)
{
$sub_array = array();
$sub_array[] = $row["runtime_hours1"];
$sub_array[] = $row["runtime_hours2"];
$sub_array[] = $row["runtime_hours3"];
$data[] = $sub_array;
}
$output = array(
"data" => $data
);
echo json_encode($output);
?>
</code>
<code><?php include('db.php'); include('function.php'); //error_reporting(E_ALL); //ini_set('display_errors', 1); $compressor_hours = ''; //get the last read runtime hours of each compressor //return one row of plant_readings_id | runtime_hours1 | runtime_hours2 | runtime_hours3 $compressor_hours .= "SELECT MAX(r.id), MAX(CASE c.compressor_name WHEN 'CM101' THEN c_r.runtime_hours END) AS runtime_hours1, MAX(CASE c.compressor_name WHEN 'CM201' THEN c_r.runtime_hours END) AS runtime_hours2, MAX(CASE c.compressor_name WHEN 'CM301' THEN c_r.runtime_hours END) AS runtime_hours3 FROM plant_readings r LEFT JOIN compressor_readings c_r ON c_r.plant_readings_id = r.id LEFT JOIN compressors c ON c_r.compressor_id = c.id;"; $statement = $connection->prepare($compressor_hours); $statement->execute(); $result = $statement->fetchAll(); $data = array(); $filtered_rows = $statement->rowCount(); foreach($result as $row) { $sub_array = array(); $sub_array[] = $row["runtime_hours1"]; $sub_array[] = $row["runtime_hours2"]; $sub_array[] = $row["runtime_hours3"]; $data[] = $sub_array; } $output = array( "data" => $data ); echo json_encode($output); ?> </code>
<?php
include('db.php');
include('function.php');
//error_reporting(E_ALL); 
//ini_set('display_errors', 1);
$compressor_hours = '';
//get the last read runtime hours of each compressor
//return one row of plant_readings_id | runtime_hours1 | runtime_hours2 | runtime_hours3
$compressor_hours .= "SELECT MAX(r.id),
MAX(CASE c.compressor_name WHEN 'CM101' THEN c_r.runtime_hours END) AS runtime_hours1, 
MAX(CASE c.compressor_name WHEN 'CM201' THEN c_r.runtime_hours END) AS runtime_hours2, 
MAX(CASE c.compressor_name WHEN 'CM301' THEN c_r.runtime_hours END) AS runtime_hours3
FROM plant_readings r 
LEFT JOIN compressor_readings c_r ON c_r.plant_readings_id = r.id
LEFT JOIN compressors c ON c_r.compressor_id = c.id;";
 
$statement = $connection->prepare($compressor_hours);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
$filtered_rows = $statement->rowCount();

foreach($result as $row)
{
    $sub_array = array();
    $sub_array[] = $row["runtime_hours1"];  
    $sub_array[] = $row["runtime_hours2"];  
    $sub_array[] = $row["runtime_hours3"];  

     $data[] = $sub_array;
}
$output = array(
    "data" => $data
);
echo json_encode($output);
?>

Then the JS

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$(document).ready(function(){
$('#add_button').click(function(){
//$('#plant_readings_form')[0].reset();
$('.modal-title').text("Add New Readings");
$('#action').val("Add");
$('#operation').val("Add");
var runtime_hours1 = $(this).attr("runtime_hours1");
var runtime_hours2 = $(this).attr("runtime_hours2");
var runtime_hours3 = $(this).attr("runtime_hours3");
$.ajax({
url:"fetch_prefill_data.php",
method:"POST",
data:{runtime_hours1: runtime_hours1,
runtime_hours2: runtime_hours2,
runtime_hours3: runtime_hours3},
dataType:"json",
success:function(data)
{
$('#runtime_hours1').attr('value',data.runtime_hours1);
$('#runtime_hours2').attr('value',data.runtime_hours2);
$('#runtime_hours3').attr('value',data.runtime_hours3);
$('#plant_readings_form')[0].reset();
}
})
});
</code>
<code>$(document).ready(function(){ $('#add_button').click(function(){ //$('#plant_readings_form')[0].reset(); $('.modal-title').text("Add New Readings"); $('#action').val("Add"); $('#operation').val("Add"); var runtime_hours1 = $(this).attr("runtime_hours1"); var runtime_hours2 = $(this).attr("runtime_hours2"); var runtime_hours3 = $(this).attr("runtime_hours3"); $.ajax({ url:"fetch_prefill_data.php", method:"POST", data:{runtime_hours1: runtime_hours1, runtime_hours2: runtime_hours2, runtime_hours3: runtime_hours3}, dataType:"json", success:function(data) { $('#runtime_hours1').attr('value',data.runtime_hours1); $('#runtime_hours2').attr('value',data.runtime_hours2); $('#runtime_hours3').attr('value',data.runtime_hours3); $('#plant_readings_form')[0].reset(); } }) }); </code>
$(document).ready(function(){
    $('#add_button').click(function(){
        //$('#plant_readings_form')[0].reset();
        $('.modal-title').text("Add New Readings");
        $('#action').val("Add");
        $('#operation').val("Add");
        var runtime_hours1 = $(this).attr("runtime_hours1");
        var runtime_hours2 = $(this).attr("runtime_hours2");
        var runtime_hours3 = $(this).attr("runtime_hours3");
        $.ajax({
            url:"fetch_prefill_data.php",
            method:"POST",
            data:{runtime_hours1: runtime_hours1,
                  runtime_hours2: runtime_hours2,
                  runtime_hours3: runtime_hours3},
                
            dataType:"json",
            success:function(data)
            {
                $('#runtime_hours1').attr('value',data.runtime_hours1);
                $('#runtime_hours2').attr('value',data.runtime_hours2);
                $('#runtime_hours3').attr('value',data.runtime_hours3);
                $('#plant_readings_form')[0].reset();
            }
        })

    });

Hardcoding will set the value in the field.
$('#runtime_hours1').attr('value',1);

I debug by with document.write to get the following

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$('#runtime_hours1').attr('value',document.write(data.runtime_hours1));
$('#runtime_hours1').attr('value',document.write(data));
...
undefined
[object Object]
</code>
<code>$('#runtime_hours1').attr('value',document.write(data.runtime_hours1)); $('#runtime_hours1').attr('value',document.write(data)); ... undefined [object Object] </code>
$('#runtime_hours1').attr('value',document.write(data.runtime_hours1));
$('#runtime_hours1').attr('value',document.write(data));
...
undefined
[object Object]

The code looks fine, so I don’t understand why its returning undefined?
Thanks in advance

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật