0

Why does this code work fine in the unit test, but not in the page? I have Firebug and FirePHP in place and can see the variable pass just fine if I hard code it, the operation is passing an int just fine in the unit test, but I've tried parseInt, Math.floor, and many other wacky methods and the value for statementCount simply won't post. The ajax:

            //polling code
        var statementCount = 0; 

        (function poll(){
        setTimeout(function(){

        $.ajax({ url: "RROO_update.php", 
                type: "POST", 
                data: {'meetID': 2176, 'statementCount': statementCount}, 
                    success: function(data){

                        if(data.length > 0){

                            var statements = JSON.parse(data);

                            //reset the statement count
                            statementCount = statementCount + statements.length;

                            $(this).actplug_RROO('formatReturns', statements, userID);
                            poll();
                        }                                   

                     },         
                    error: function(){
                        poll();
                    }, 
                });
        }, 5000);
        })();

and the php:

<?php
 include("../inc/variables.php");
 error_reporting (E_ALL ^ E_NOTICE);

 require_once('../FirePHPCore/FirePHP.class.php');
 require_once('../FirePHPCore/fb.php');
 $firephp = FirePHP::getInstance(true);
 ob_start();



 $MeetingID         = $_POST['meetID'];
 $StatementCount    = (int)$_POST['statementCount'];


 $firephp-> log($StatementCount, 'Statement count passed in' );

 $Finished = FALSE;

 while($Finished == FALSE){

     $MeetingStats = mysql_query("SELECT RROO_UPDATE.*, MEMBER.UserName, MEMBER.UserImage FROM RROO_UPDATE JOIN MEMBER ON RROO_UPDATE.MemberID = MEMBER.MemberID WHERE MeetingID = $MeetingID ORDER BY TimeStamp DESC", $DB_Connection);
     $MyNum = mysql_num_rows($MeetingStats);
     $firephp->log($MyNum, 'Row Query');


     if($MyNum > $StatementCount){

     $Returns = array();

        while($Return = mysql_fetch_array($MeetingStats)){      
            array_push($Returns, $Return);      
         } 

         $NewReturns = array();
         $NewStats = $MyNum - $StatementCount;

         $firephp->log($NewStats, 'heres the new stats count');

         for($i = 0; $i < $NewStats; $i++){

            array_push($NewReturns, $Returns[$i]);

         }
         $Here = count($NewReturns);
         $firephp->log($Here, 'The length of the new returns array');

         $Finished = TRUE;
         echo json_encode($NewReturns);

     }

     else{
        sleep(3);
     }

 }       

?>

Like I said, it comes back fine on the unit test which is the same in all the aspects I can see (I actually copy pasted it into the page) the only difference being that the postback is routed differently on the page (to the plugin) but I've messed around with callback to no avail. Is there some reason the statementCount won't reset and Post in this code?

tradyblix
  • 7,439
  • 3
  • 25
  • 29
Ele Munjeli
  • 446
  • 1
  • 7
  • 15

1 Answers1

0

I don't think that statementCount is defined inside the callback, only in the function which executes the ajax call.

Here is a question and answer which should help you do what you want.

Community
  • 1
  • 1
fredrik
  • 6,483
  • 3
  • 35
  • 45
  • The code is working in a unit test with this structure, so I don't think that's it. And it works fine if I hard code a number like, statementCode = 150; in the same place. It's the operation that is making the problem I think. – Ele Munjeli Mar 12 '13 at 07:25
  • I've done that. The js fails silently, suggesting the issue is in the way it's posting to the php file. You can see the js is valid code, and there's debugging code in the php as well. So what I do know is that for some reason, the failure appears to be on the operation in this file (statementCount + statements.length;) although the same operation is working fine on the unit test. – Ele Munjeli Mar 12 '13 at 17:40
  • Does the unit test actually make a ajax call or is it stubbed? If you add a error callback to the ajax call, will that be called instead? – fredrik Mar 12 '13 at 17:43
  • The code for the unit test in the poll method is exactly the same except for the method the returned array is passed to: it prints instead of going to the actplug_RROO plugin. However, the method is hanging on the submission to the php- there's no response and it doesn't print the php to the logs. It seems to be hanging on the POST and appears to be a type error, but the type is correct, it just won't run there. I tried putting the poll() and setting the statementCount on the callback to actplug_RROO but that didn't work either. – Ele Munjeli Mar 13 '13 at 00:26
  • Does the client try to make a connection to the server? Perhaps the server-side script has a bug. – fredrik Mar 13 '13 at 05:54
  • That's what the PHP debugging and mysql error code is for. But I don't see via the code why the integer isn't passing. It works in the same place you understand with a hard coded integer; the question is why simply the operation within the ajax isn't making a viable number. That's where the fail is. – Ele Munjeli Mar 20 '13 at 22:37