PHP Doku:: Serialisiert einen einzelnen Wert in ein WDDX-Paket - function.wddx-serialize-value.html

Verlauf / Chronik / History: (3) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzXML-ManipulationWDDXWDDX Funktionenwddx_serialize_value

Ein Service von Reinhard Neidl - Webprogrammierung.

WDDX Funktionen

<<wddx_packet_start

wddx_serialize_vars>>

wddx_serialize_value

(PHP 4, PHP 5)

wddx_serialize_value Serialisiert einen einzelnen Wert in ein WDDX-Paket

Beschreibung

string wddx_serialize_value ( mixed $wert [, string $kommentar ] )

wddx_serialize_value() wird verwendet um einen einzelnen Wert in ein WDDX-Paket zu serialisieren. Übergeben wird der Wert in wert und ein optionaler Parameter kommentar, der im Header des WDDX-Pakets erscheinen wird. Zurückgegeben wird das entsprechende WDDX-Paket.


Ein BenutzerBeitrag:
- Beiträge aktualisieren...
chris at digitalbase dot com
5.01.2002 1:36
When serializing JavaScript objects to interact with PHP objects, you should make the JavaScript object's first entry a php_class_name.

PHP:
class myclass {
    var $name;
    var $age;
    var $ssn;
    ...
   
    function myclass() {
        $this->name = "";
        $this->age = "";
        $this->ssn = "";
        ...
    }
}

JavaScript:
// serialization example code is from the
// WDDX SDK http://www.openwddx.org
function SerializeMsg(name, age, ssn, ...) {
    var MyObj = new Object;
   
    // the following line is necessary in order
    // to deserialize back to a PHP object
    MyObj.php_class_name = "myclass";
   
    MyObj.name = name;
    MyObj.age = age;
    MyObj.ssn = ssn;
    ...
   
    // Create a new serializer "object" to do our work for us
    MySer = new WddxSerializer;

    // Serialize the Message variable into a WDDX Packet
    MyWDDXPacket = MySer.serialize(MyObj);

    // Output WDDX Packet so we can see what it looks like
    return MyWDDXPacket;
}

Now you can use this in a page which is its own form action:
<?php
// don't forget to include the class definition from above!
if (!isSet($wddx_packet))
   
$MyObj = new myclass();
else
   
$MyObj = wddx_deserialize($wddx_packet);
?>

<html>
<head>
<script language="JavaScript">
<!--
function Init() {
    // sets the form's text-elements
    tmp = document.MyForm;
   
    tmp.name.value = "<? echo $MyObj->name ?>";
    tmp.age.value = "<? echo $MyObj->age ?>";
    tmp.ssn.value = "<? echo $MyObj->ssn ?>";
    ...
}

function SerializeMsg(name, age, ssn, ...) {
// see above!
}

function SubmitForm() {
    // sumbits the form
    tmp = document.MyForm;
   
    tmp.wddx_packet.value =
    SerializeMsg(tmp.name.value, tmp.age.value, tmp.ssn.value,...);
    document.submit();
}
// -->
</script>
</head>
<body>
...
<form name="MyForm"
action="<? echo $PHP_SELF ?>" method="post">
<input name="wddx_packet" type="hidden">
<input name="name" type="text">
<input name="age" type="text">
<input name="ssn" type="text">
...
<input name="SubmitBtn" type="button" onclick="SumbitForm()">
</form>
</body>
</html>

---------------------------------
If you don't provide that first 'php_class_name' in the JavaScript function, then when PHP deserializes it will deserialize into an associative array and not an object instance.

Note that this provides a way to maintain state without
using sessions or cookies - just good old HTML!

Hope this helps!
--
Chris Hansen



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)