0

I have this game I'm making and I'm using ajax call to php to get items from the database, but I getting getting an error so the preloader doesn't go away, and I get this error when i view the resources on Chrome : "Uncaught TypeError: Cannot read property 'team' of null". Any idea why it's doing this? Jquery:

function appendTeam(){

$.ajax({

       url : _path + "/core/ajax.php",
       type : 'POST',
       data : { f: 'getTeam'},
       dataType : 'json',
       success :  function(data) {

        if(data) {


            return false;
        } else{
            var count = 0;
            $.each(data.team, function(i, c){
                // check
                if(!$('#'+c)) return true;
                var element = $('#'+c);
                $('input[name="s'+i+'"]').val(element.attr('id'));
                $('.slot.'+(i+1)).append(element);
                element.data('prevParent', $('.slot.'+(i+1)));
                count ++;

            });

            appendStatus(count);
            setTimeout(function(){
                $('#preloader').fadeOut('fast',function(){
                    $('#preloader').remove();
                    popUp('match');
                });
            }, 2000);


        }

    }
});
}

Php:

if (isset($_POST['getTeam'])) {
$team = array();
$sql = mysql_query("SELECT * FROM accounts WHERE id = '1'")  or die(mysql_error());


while ($teams = mysql_fetch_array( $sql )) {

$chara1 = $team['cid1'];
$chara2 = $team['cid2'];
$chara3 = $team['cid3'];

}
$team = json_encode(array(
'chara1' => $chara1,
'chara2' => $chara2,
'chara3' => $chara3
));
echo $team;
}

And I want it to echo the team, if he has a team already selected in these div elements :

<div id="droppable_slots" class="current_team">
                    <div class="slot 1">1</div>
                    <input type="hidden" name="s0" value="10">
                    <div class="slot 2">2</div>
                    <input type="hidden" name="s1" value="7">
                    <div class="slot 3">3</div>
                    <input type="hidden" name="s2" value="3">
                </div>
Dj Ike
  • 25
  • 1
  • 1
  • 7
  • I don't think your PHP code is returning valid JSON, and even if it were I don't see anything creating an object property called "team". – Pointy Oct 08 '13 at 19:29
  • How would i make it so that it returns a valid JSON? – Dj Ike Oct 08 '13 at 19:30
  • You probably mean `if( !data) {` Also, return statements do not work in `success` – karthikr Oct 08 '13 at 19:30
  • the error says `data` is null, i suggest upgrading jQuery so that in this case jQuery will properly throw an error message `parseerror` since your json is invalid. – Kevin B Oct 08 '13 at 19:34
  • @KevinB actually I misread the code. The code as it stands won't really do very much; the `while` loop references "$team" instead of "$teams" to look at each row, and "$team" is therefore always empty. The loop just echoes out a single object with empty values. Basically, it's kind-of a long way from working. – Pointy Oct 08 '13 at 19:35
  • isn't the error happening in the javascript, not the php? well, the error he's reporting anyway. property `team` of `null` suggests to me that `data` is null since that is the only place where he's using `team` as a property in the javascript. And, if `data` is null, then he's using an older version of jquery and the php returned nothing. – Kevin B Oct 08 '13 at 19:38
  • @KevinB if you look at the PHP (and note that I'm not a PHP programmer, but this seems pretty obvious), there's just one thing echoed to the output, and that's a single JSON object without a property called "team". – Pointy Oct 08 '13 at 19:39
  • Right, but it's not saying team is undefined on an object, it's saying `team` is undefined on `null` *"Cannot read property 'team' **of null**"* But yes, there are errors in the php that are likely leading to data being null.. – Kevin B Oct 08 '13 at 19:41

2 Answers2

0

You're looking for the wrong key in the post data -- it should be "f", not "getTeam". (Note that the Ajax call is sending data : { f: 'getTeam'}.) Presumably you meant this:

if (isset($_POST['f']) && $_POST['f'] == 'getTeam') 

Another typo: if (data) return false will return false when there is data -- should be the opposite:

if(data && data.team) {
    // do stuff
}
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • It works but now i get this :Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/kenchix1/public_html/game/core/ajax.php on line 19 {"chara1":null,"chara2":null,"chara3":null} – Dj Ike Oct 08 '13 at 19:36
  • @karthikr not sure what you mean -- `return` won't exit the function? It doesn't really matter anyway in this case, as the "else" block will still not get executed. – McGarnagle Oct 08 '13 at 19:37
  • This is a little redundant. If data doesn't exist, then data.team for sure doesn't exist! Why check for both? – Charles D Pantoga Oct 08 '13 at 19:39
  • @karthikr that's a separate problem -- it probably suggests that your query failed somehow. See here: http://stackoverflow.com/a/2973209/1001985 – McGarnagle Oct 08 '13 at 19:40
  • returning false or true doesn't matter, the point is it exits the function. – Kevin B Oct 08 '13 at 19:43
  • @KevinB ok point taken, but it's somewhat tangential to the OP -- the key point is that the function should proceed if there is data. – McGarnagle Oct 08 '13 at 19:46
  • Yes, i was agreeing with you more or less. re-inforcing that returning anything (true or false or nothing) will exit the function, which is what is (was) wanted in that situation. you've since updated to not use a return. – Kevin B Oct 08 '13 at 19:47
0

I think your problem is here:

if (data) {  
    return false;   // returns false if data exists
} else {
   // data does not exist in this block
   var count=0;
   $.each(data.team ...
   ... //
}

Basically, because of your if/else block isn't formed properly. This should fix it...

if (!data) {
    return false;
} else {
    ... // work with data here
}

also you need this:

mysql_fetch_array($con, $sql);

If you are going to use the procedural functions you will need to pass your connection resource ($con = mysql_connect) to the function. If you use the object oriented approach, you do not need to pass the resource as a parameter.

Charles D Pantoga
  • 4,307
  • 1
  • 15
  • 14