0

I am using the Chrome Debugger, and can see that I am correctly getting the JSON feed from the AJAX request (PHP File -> Page). The issue is taking the data from that feed, and inserting it into the table mentioned below. The part that confuses me most, is that this code (nearly exactly the same) works perfectly on another part of the site, but doesn't seem to work here.

JSFiddle: http://jsfiddle.net/2qc01ceL/

HTML:

<div class="form_head">Flight & Duty</div>

<table cellpadding="4" cellspacing="0" width="100%">

<thead>
<tr class="tab_head" style="color:#FFF; text-align:center"><th rowspan="2">Pilot</th><th rowspan="2">ARN</th><th colspan="2">Remaining Duty Time</th><th colspan="3">Remaining Flight Time</th></tr>
<tr class="tab_head" style="color:#FFF; text-align:center"><th>7 Day</th><th>14 Day</th><th>7 Day</th><th>30 Day</th><th>365 Day</th></tr>
</thead>

<tbody>
<tr><td id="name"></td><td id="arn"></td><td></td><td></td><td></td><td></td><td></td></tr>
</tbody>

</table>

JQuery/AJAX:

$(document).ready(function() {

var data = function(callback) {
    return $.ajax({
        type: "GET",
        dataType: 'json',
        url: "required/duty.php",
        async: false,
        success: function(doc) {
            var duty = [];
            $(doc).each(function() {
                duty.push({
                    id: $(this).attr('id'),
                    dutyarn: $(this).attr('dutyarn'),
                    dutyon: $(this).attr('dutyon'),
                    dutyoff: $(this).attr('dutyoff'),
                    dutytime: $(this).attr('dutytime'),
                    userfname: $(this).attr('userfname'),
                    userlname: $(this).attr('userlname'),
                    });
                });
                callback(duty);
            }
        }).responseText;
    };

    document.getElementById("arn").innerHTML = duty.arn;    
}); 

PHP File:

mysql_connect(
"mysql.**.com", 
"**", 
"**") or die
("Unable to connect to SQL server");

mysql_select_db(
"**") or die
("Unable to select database");

$json = array();

$get_duty = "
SELECT * FROM heli_duty 
LEFT JOIN heli_user ON duty_arn = user_arn
WHERE 1";

$run_duty = mysql_query("$get_duty") or die($sql_error .mysql_error());

$duty_array = array();

        while ($duty = mysql_fetch_assoc($run_duty, MYSQL_ASSOC)) {

            $duty_array[] = array(
                'id' => $duty[duty_id],
                'dutyarn' => $duty[duty_arn],
                'dutyon' => $duty[duty_on],
                'dutyoff' => $duty[duty_off],
                'dutytime' => $duty[duty_time],
                'userfname' => $duty[user_fname],
                'userlname' => $duty[user_lname],
            );
        }
    echo json_encode($duty_array);
James Ham
  • 119
  • 7
  • Also please look into MySQLi as the standard MySQL is deprecated and will no longer be maintained, as well as being flawed from a security point of view. – Martin Jan 21 '15 at 11:29
  • Thanks @Martin - but due to circumstances, I can not use MySQLi on this project. – James Ham Jan 21 '15 at 11:30
  • are you able to elaborate? I'm trying to think of a project reason where MySQL > MySQLi but I can't :-/ – Martin Jan 21 '15 at 11:33
  • @Martin - It is at the request of the domain owner.. – James Ham Jan 21 '15 at 11:35
  • wow. just, wow. I mean, you can use MySQLi in a procedural context, and a bunch of search and replace clauses such as http://stackoverflow.com/a/28049884/3536236 means you can upgrade your PHP/MySQL pretty easily in an afternoon. Or less. – Martin Jan 21 '15 at 11:40
  • @Martin - Thanks for your input but I understand the differences and have explained these to the Domain Owner, but they wish, for their own personal reasons to remain MySQL.. Do you have any input on the mentioned topic? – James Ham Jan 21 '15 at 11:42

2 Answers2

0

The probleme is the way you parse your json data, you parse it like a dome element, but it's a json array! try something like this:

$.each(doc, function(i, item) {
    duty.push(item);
});

here a fiddle : http://jsfiddle.net/sLmotxea/1/

Hope I help!

ekans
  • 1,662
  • 14
  • 25
  • I don't understand what you mean exactly? (sorry) – James Ham Jan 21 '15 at 11:31
  • $(doc).each(function() { duty.push({ id: $(this).attr('id'), dutyarn: $(this).attr('dutyarn'), dutyon: $(this).attr('dutyon'), dutyoff: $(this).attr('dutyoff'), dutytime: $(this).attr('dutytime'), userfname: $(this).attr('userfname'), userlname: $(this).attr('userlname'), } – ekans Jan 21 '15 at 11:33
  • Have tried this, but am now getting `Uncaught ReferenceError: duty is not defined` – James Ham Jan 21 '15 at 11:38
0

can you confirm the value of duty.arn before sending it to the table, with an alert?

Try replacing the document.getElementById("arn").innerHTML with a similar function such as .html() ?

Martin
  • 22,212
  • 11
  • 70
  • 132