note 59117 deleted from function.serialize by aharvey
| From: | aharvey@php.net | Date: | Tue, 06 Nov 2012 02:09:59 +0000 |
| Subject: | note 59117 deleted from function.serialize by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-191766@lists.php.net to get a copy of this message | ||
Note Submitter: andrea at 3site dot it
----
Hello!
Here you can find a JavaScript Object that should serialize JS vars for PHP and unserialize PHP vars
for JavaScript.
http://www.devpro.it/javascript_id_102.html
Please read notes inside the code to know more about vars compatibility.
It was created to resolve AJAX interaction and maybe usefull for other situations .... such:
<?php
// example nested array
$myarray = array(
true,
123,
array(
'nested',
array(
1,
'test'=>false
)
),
'other'=>'value'
);
// array serializzation
$serialized = serialize($myarray);
?>
<script type="text/javascript" src="PHP_Serializer.js"></script>
<script type="text/javascript">
// function to dump an object
function showObject(obj, level) {
var l = '';
var tmp = '';
for(var a = 0; a < level; a++)
l += '[_]';
for(var a in obj) {
var type = typeof(obj[a]);
if(type == 'object')
tmp += showObject(obj[a], (level + 1));
else
tmp += l + ' key: ' + a + ' = value: ' +
obj[a] + ' < ' + type + '\n';
}
return tmp;
}
// new PHP_Serializer variable
var php = new PHP_Serializer();
// dinamyc JS var creation
var myJSvar = php.unserialize("<?php echo str_replace('"',
'\\"', $serialized); ?>");
// look at dump :-)
alert(showObject(myJSvar, 0));
</script>
Regards