Topic: Variable type preservation between PHP and JavaScript

It would be a good idea to have a way to preserve variable types between PHP and JavaScript and vice versa, especially for boolean values.

Currently when I make a xajax call from JavaScript, a boolean false will be passed as the string "false" to the PHP function at the server side, which then equals to a boolean true. Also when I use the addScriptCall method of the xajaxResponse class, any boolean values passed as parameters will become strings (either "true" or "false") at the client side.

It would be a huge improvement if this could be resolved.

Re: Variable type preservation between PHP and JavaScript

very "true" tongue
for now, you can use 0 and 1 instead of false and true?

Re: Variable type preservation between PHP and JavaScript

Booleans are indeed a bit of a pain right now. I plan on working on that soon.

Project manager for xajax

Re: Variable type preservation between PHP and JavaScript

Me too.  I'll race you tongue

Re: Variable type preservation between PHP and JavaScript

Hey, go ahead, I'm in no hurry. smile

Project manager for xajax

Re: Variable type preservation between PHP and JavaScript

Hi. I don't know whether this counts as bumping or not - one can never tell - but there's a half solution to this problem, at least on the server-side of things.

function CheckTypes(&$var) {
    if(strtoupper($var) === 'FALSE') { return (bool) FALSE; }
    if(strtoupper($var) === 'TRUE') { return (bool) TRUE; }
    return $var;
}

... and for some reason I have a reference thing in there. Oh well. Anyway, just run that on any variables you expect to evalulate as booleans. But I'm sure you knew that already. smile

7 (edited by Cyberwolf 2006-05-02 6:15:22 PM)

Re: Variable type preservation between PHP and JavaScript

Will this enhancement be part of 0.5? :-)

On the PHP backend I had to return boolean values (which I passed to a xajax call) back to a JavaScript function with $objResponse->addScriptCall. I was already using 1 and 0 for the xajax call which equals to a boolean true or false on the PHP side, but when I pass them back to a JavaScript function they always equals to a boolean true (JavaScript's type casting behaves different).

I've used the following JavaScript code to convert "0" or "1" (here the variable is called direction) to a boolean false or true:

if ( typeof direction == 'string' ) {
    var directionInt = parseInt(direction);
    var directionBool = new Boolean(directionInt);
    direction = directionBool.valueOf();
}

Re: Variable type preservation between PHP and JavaScript

Yes, we hope to have the PHP-Javascript variable passing get more accurate in the 0.5 code.

Project manager for xajax

Re: Variable type preservation between PHP and JavaScript

Any news on this one? Since 0.5 is coming closer, I worry if this will be included or not.

Maybe it would be a good idea to have a page in the wiki describing the feature requests you're wanting to implement, for which release and in which priority. It's currently very hard for me to keep eyes on the project's progress and future.

Re: Variable type preservation between PHP and JavaScript

That's a good idea, Cyberwolf. I'm having a hard time keeping track of it myself, which is rather scary actually.

Project manager for xajax

Re: Variable type preservation between PHP and JavaScript

Any news on this? I didn't test this yet in 0.5 beta 2.

12 (edited by vdboor 2007-04-03 3:43:06 PM)

Re: Variable type preservation between PHP and JavaScript

I'm currently using a variant like:

var modifiedArgs = [];
for( var i = 0; i < arguments.length; i++ ) {
  var value = arguments[i];
  if( value === true ) { value = 1; }
  else if( value === false ) { value = 0; }

  modifiedArgs[i] = value;
}

I currently use this approach in a wrapper to xajax.call()

Software developer and Open Source contributor.
Working on KMess, a MSN Messenger client for Linux.