开发者

Getting some weird results with jQuery / PHP / MySQL

I'm having trouble getting the expected result.

I have the following jQuery script:

   // obj is the return result of another query
  jQuery.post("mypath/include/jquery_bll.php", { instance: 'controllAccess', brandID: obj.id },
  function(data)
  {
    if (jQuery('#brand_selectList').length == 0)
    {
      if (data == "Locked")
      {
        jQuery('#countryListBox').attr('disabled','true');
      }
    }
  }, "text");

So what this does, is to disable select box if return result = "Locked".

Here is my PHP code that returns the result:

 function controllBrandRelation($dal)
  {
    // Get label data.
    $result = $dal->getRowByValue('myTable','id', $_POST['someID']);

    if (mysql_num_rows($result) > 0)
    {
      while ($row = mysql_fetch_array($result))
      {
        $ownerID = $row["fk_userID"];    
      }
    }

    if(!empty($ownerID))
     echo 'Locked';
    else
      echo 'Open';       
  }

This will return 'Locked' if $ownerID has some values.

This is my problem:

If I do an alert(data.length), I get the result 72. I really should be getting 6, which is the number of characters in the word 'Locked'.

  jQuery.post("mypath/include/jquery_bll.php", { instance: 'controllAccess', brandID: obj.id },
  function(data)
  {
    alert(data.length);
  }

If I do an alert(jQuery.trim(data).length), I get the correct value.

But whatever I do, I'm not able to get inside this IF test:

  if (data == "Locked")
  {
    jQuery('#countryListBox').attr('disabled','true');
  }

Why is data not equal "Locked"?

I've tested with both the trimmed and untrimed version.

UPDATE

I've managed to get it working by changing return results from text to json. So now I check on if (data.status == "locked"), and it works fine.

But I really would like to know how the return results for text is 76 characters long, when it 开发者_StackOverflowshould be 6. And why I'm not able to match/test the result.


First, I'm not sure of whether your are terminating the script after you echo your return values to the AJAX calling function. You could remedy that by doing the following:

function controllBrandRelation($dal) {
    // Get label data.
    $result = $dal->getRowByValue('myTable','id', $_POST['someID']);
    if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_array($result)) {
            $ownerID = $row["fk_userID"];    
        }
    }
    echo (!empty($ownerID)) ? 'Locked' : 'Open';
    die;

}

Second, if you are able to successfully trim the value down to 6, you should try to store that value and use that in your check. Have you tried alerting your value to see if it matches yet?

jQuery.post("mypath/include/jquery_bll.php", { instance: 'controllAccess', brandID: obj.id }, function(data) {
    data = jQuery.trim(data);
    alert(data);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜