views:

34

answers:

1

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 should be 6. And why I'm not able to match/test the result.

A: 

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);
}
cballou
The PHP function works as expected and it returns what I want. I test this by echoing the result. I'm writing an update on my question now - take a look in about 3 minutes.
Steven
If you have firebug installed on firefox, you can check your return response in the Console and see just how many characters long it is. I see no problems with your PHP, could there possible by something else within that file other than the controllBrandRelation() function which is echoing blank spaces?
cballou