1 (edited by Charset 2011-01-18 1:38:36 PM)

Topic: alert string returns null (UTF8? problem)


i tried upgrading from v 0.5 to 0.6 and having some strange problems. I have a function that reads data from MySQL and creates a string containing html code which then gets assigned by innerHTML to the DOM.

In v0.5 everything works fine. In v0.6 there is nothing returned.

I tried to alert the html code which returned "null" even though the string definately contains html code.

After reading your sticky note about the UTF8 problem, i tried to utf8_encode the html string first before returning ist - like this: 
$objResponse->assign( 'id', 'innerHTML', utf8_encode( $htmlString ) );

that Worked fine. But as a matter of fact my PHP application has so much code that it would not be a good idea to replace every part where i'm using objRespnse->assign with that utf8_encode.

Is there an easy way to deal with this issue - maybe some global configuration that everything it converted to utf8 automatically?


Re: alert string returns null (UTF8? problem)


I'm not sure if this works in general, but it worked for me (so far), and you can try it if you want:

I changed the assign function in xajax_core/xajaxResponse.inc.php and added the utf8_encode there, so it looked like this in the end:

public function assign($sTarget,$sAttribute,$sData)
        return $this->addCommand(

I hope this works in general for us both

Re: alert string returns null (UTF8? problem)


thanks for the reply. I will try that after my vacation. I try to remember to post my experience here.

Re: alert string returns null (UTF8? problem)

The utf8_encode() function works fine for showing special characters on screen, not only for the strings in your code but for data brought from the database as well. Remember though that you don't only print in screen (and other places) by the command "assign" only, so go ahead and change append and prepend too.  On these 3 code looks pretty much the same:


But on other commands like alert, contentAssign, contentAppend, contentPrepend I ignore if it would be necessary to change to utf8_encode() (guess you would need it too).

5 (edited by laraweb 2012-02-13 3:05:09 PM)

Re: alert string returns null (UTF8? problem)

In xajax 0.6 I have successfully solved the special characters problem (á, é, í, ó, ú, ñ) by modifying on xajaxResponse.inc.php at functions assign, prepend, append by changing the line $sData for utf8_encode($sData). This seems to work pretty good but on instances where I need to use a method like:

$objResponse->call('autoFormAssign', $key, utf8_encode($value));

I have to enclose the parameters (the value of an input box in this case) sent to the javascript function with ut8_encode. 

Is there a way to modify the original code  (xajaxResponse.inc.php) so that the utf8_encode can be automatically made to parameters sent?


(I've been a user of this forum since 2006 and I'm still a newbie?  Talk about high standards!  I should be a grandpappy by now.)

Re: alert string returns null (UTF8? problem)

starts from: line 1613

public function addCommand($aAttributes, $mData)
        if ('text/xml' == $this->getContentType()) $mData = $this->_encodeArray($mData);;
        /* merge commands if possible */
        if ( in_array($aAttributes['cmd'],array('js','ap') ) )
            if ($aLastCommand = array_pop($this->aCommands))
                if ($aLastCommand['cmd'] == $aAttributes['cmd'])
                    if ('js' == $aLastCommand['cmd'])
                        $mData = $aLastCommand['data'].'; '.$mData;
                    elseif('ap' == $aLastCommand['cmd'] && $aLastCommand['id'] == $aAttributes['id'] && $aLastCommand['prop'] == $aAttributes['prop'])
                        $mData = $aLastCommand['data'].' '.$mData;
                        $this->aCommands[] = $aLastCommand;
                } else {
                    $this->aCommands[] = $aLastCommand;
/*here is an good place such as


        $aAttributes['data'] = $mData;
        $this->aCommands[] = $aAttributes;

        return $this;

Re: alert string returns null (UTF8? problem)

I have found the following things to be quite useful:

  • Use a unicode database collation utf8_general_ci before you populate your database

  • Execute mysql_query("SET NAMES 'utf8'"); before your actual queries

  • Save your PHP files encoded in utf8 without BOM instead of ANSI before these contain any special characters

  • Send a header header('Content-type: text/html; charset=utf-8');

then you will not have to encode/decode your data all over the place, and can happily display all sorts of characters.

Re: alert string returns null (UTF8? problem)

Pixel is right.

I do NOT recommend patching utf8_encode()  into the xajax core. That's the totally wrong approach.

Instead u should make sure that ALL your sources of data ALL have the same charset. When you fetch data from a database, make sure the tables, fields and connection collation is set to UTF8.
When you return hard coded strings like $objResponse->alert('Hello special chars äöü'); make sure your file is UTF8 encoded as well.

As long as u keep everything in UTF8 there's no need to use utf8_encode() inside of your scripts, and if you do have to use utf8_encode - to the before calling ->append/prepend/assign.