jacksun101
jacksun101

Reputation: 400

Autocomplete error, null is not an object, IE only

the following code works well in Chrome/FireFox, but error in IE. The error shows the message.

Message: 'name' is null or not an object

I know there are quite a few questions related. But they seem to be different in some way. So, please help. Thanks. (The error is occurred at line 7)

    $("#keywords").autocomplete('downloads/get_keywords', {
        multiple: true,
        parse: function(data) {
            return $.map(eval(data), function(row) {
                return {
                    data: row,
                    value: row.name, // error in here.
                    result: row.name
                };
            });
        },
        formatItem: function(item) {
            return format(item);
        }
    });

Upvotes: 1

Views: 2403

Answers (2)

Bruno Alves
Bruno Alves

Reputation: 11

Try to use these functions:

function json_encode_string($in_str)
{
  mb_internal_encoding("UTF-8");
  $convmap = array(0x80, 0xFFFF, 0, 0xFFFF);
  $str = "";
  for($i=mb_strlen($in_str)-1; $i>=0; $i--)
  {
    $mb_char = mb_substr($in_str, $i, 1);
    if(mb_ereg("&#(\\d+);", mb_encode_numericentity($mb_char, $convmap, "UTF-8"), $match))
    {
      $str = sprintf("\\u%04x", $match[1]) . $str;
    }
    else
    {
      $str = $mb_char . $str;
    }
  }
  return $str;
}

function php_json_encode($arr)
{
  $json_str = "";
  if(is_array($arr))
  {
    $pure_array = true;
    $array_length = count($arr);
    for($i=0;$i<$array_length;$i++)
    {
      if(! isset($arr[$i]))
      {
        $pure_array = false;
        break;
      }
    }
    if($pure_array)
    {
      $json_str ="[";
      $temp = array();
      for($i=0;$i<$array_length;$i++)       
      {
        $temp[] = sprintf("%s", php_json_encode($arr[$i]));
      }
      $json_str .= implode(",",$temp);
      $json_str .="]";
    }
    else
    {
      $json_str ="{";
      $temp = array();
      foreach($arr as $key => $value)
      {
        $temp[] = sprintf("\"%s\":%s", $key, php_json_encode($value));
      }
      $json_str .= implode(",",$temp);
      $json_str .="}";
    }
  }
  else
  {
    if(is_string($arr))
    {
      $json_str = "\"". json_encode_string($arr) . "\"";
    }
    else if(is_numeric($arr))
    {
      $json_str = $arr;
    }
    else
    {
      $json_str = "\"". json_encode_string($arr) . "\"";
    }
  }
  return $json_str;
}

And the code:

echo php_json_encode($MyObject);
// The var "$MyObject" is what you want to encode as json
// If you're using PHP >= 5.20, you'll have this function implemented as "json_encode"

Upvotes: 1

jacksun101
jacksun101

Reputation: 400

This has been fixed for quite a few months. Sorry for answering this late. Be careful for the autocomplete of jQuery, the data source of array should be clean without extra comma which is easily overlooked. In my question, "downloads/get_keywords" is a custom PHP function which returns the array listed in autocomplete drop-down box. But if you are using foreach to collect your array, you probably don't care about the extra comma at the end of array. But IE(only) hates the extra comma. To tackle this, just remove it than the problem is solved.

$return_data = "";

foreach ($items as $key=>$value) {

if (strpos(strtolower($key), $q) !== false) {

  $return_data .= "{ name: \"$key\", to: \"$value\" }, ";

}

}

$return_data = substr_replace($return_data, '', -2); // IE hates extra comma

echo '[' . $return_data . ']';

Upvotes: 1

Related Questions