开发者

NaN error in ajax callback

I am getting a NaN error in my ajax callback function and can only think it has to do with an array in PHP. I have been trying to find ways to corre开发者_JAVA技巧ct it but have come up against a brick wall.

What is supposed to happen is that PHP queries the database and if there are no results send a response to ajax and issue the error message. However, all I am getting is NaN. The error stems from the success code below.

I would be grateful if someone could point out my error.

PHP code:

$duplicates = array();

foreach ($boxnumber as $val) {
    if ($val != "") {
        mysql_select_db($database_logistor, $logistor);
        $sql = "SELECT custref FROM boxes WHERE custref='$val' and status = 'In'";
        $qry = mysql_query($sql) or die(mysql_error());

        if (mysql_num_rows($qry) < 1) {
            $duplicates[] = '[ ' . $val . ' ]';
            $flag = 1;
        } else {
            $duplicates[] = $val;
        }
    }
}

//response array with status code and message
$response_array = array();
if (!empty($duplicates)) {
    if ($flag == 1) {
//set the response
        $response_array['status'] = 'error';
        $response_array['message'] = 'ERROR: ' . implode(',', $duplicates) . ' needs to be in the database to be retrived.';
    }
//if no errors
} else {

//set the response
    $response_array['status'] = 'success';
    $response_array['message'] = 'All items retrieved successfully';
    $response_array['info'] = ' You retrieved a total of: ' . $boxcount . ' boxes';
}

//send the response back
echo json_encode($response_array);

Relevant ajax:

$("#brtv-result").html(msg.message+msg.info);

jQuery code:

$(function() {

 $("#BRV_brtrv").submit(function() {

   var send = $(this).serialize();

    $.ajax({
      type: "POST",
      url: "boxrtrv.php",
      cache: false,
      data: send,
      dataType: "json",
      success: function(msg) {
       if( msg.status === 'error') {
          $("#brtv-result").fadeIn(1000).delay(1000).fadeOut(1000);
          $("#brtv-result").removeClass('error');
          $("#brtv-result").removeClass('success');
          $("#brtv-result").addClass(msg.status);
          $("#brtv-result").html(msg.message);
       }

       else {
          $("#brtv-result").fadeIn(2000).delay(2000).fadeOut(2000);
          $("#brtv-result").removeClass('error');
          $("#brtv-result").addClass('success');
          $("#brtv-result").addClass(msg.status);
          $("#brtv-result").html(msg.message+msg.info);
          //location.reload(true);
          //$('#brtv-result').addClass("result_msg").html("You have successfully retrieved: "+data.boxnumber).show(1000).delay(4000).fadeOut(4000);
          $("#BRV-brtrv-slider").val(0).slider("refresh");
          $("input[type='radio']").attr("checked",false).checkboxradio("refresh");
          var myselect = $("select#BRV-brtrv-department");
          myselect[0].selectedIndex = 0;
          myselect.selectmenu("refresh");
          var myselect = $("select#BRV-brtrv-address");
          myselect[0].selectedIndex = 0;
          myselect.selectmenu("refresh");
      }

     },
      error:function(){
         $("#brtv-result").show();
         $("#brtv-result").removeClass('success');
         $("#brtv-result").addClass('error');
         $("#brtv-result").html("There was an error submitting the form. Please try again.");
     }
   });
   return false;
  });
});


NaN (pronounced nan, rhymes with man) only happens when you try to do an operation which requires a number operand. For example, when you try to Number('man') you'll get this error.

What you return from your PHP file, is simply an array which contains simply data. So, the problem is in your JavaScript. You have to send more parts of your JavaScript, so that we can see it thoroughly.

However, I recommend that you use Firebug and set a breakpint at the correct place (the callback function start), and check the stack trace of the calls to diagnose the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜