views:

376

answers:

1

Hi all, I'm trying to send some data via dojo.xhrPost to an Zend Controller Action. I can see the data being sent in Firebug console. However, when inspecting the post data, the array is empty.

I'm not sure if it is possible to send an arbitrary string of data via dojo.xhrPost without using a form. This is probably a very n00b mistake. In any case, I'll post my code here and see what you all think.

In my layout script I have:

<?php

    $sizeurl = $this->baseUrl() . '/account/uisize';

?>

function resizeText(multiplier)
{
    if (document.body.style.fontSize == "")
    {
        document.body.style.fontSize = "1.0em";
    }
    document.body.style.fontSize = parseFloat(document.body.style.fontSize) + (multiplier * 0.1) + "em";
    var size = document.body.style.fontSize;
    var xhrArgs = {
                    url: "<?= $sizeurl; ?>",
                    postData: size,
                    handleAs: "text"
                }
    dojo.xhrPost(xhrArgs);
}

Then my action is:

public function uisizeAction()
{
    $this->_helper->viewRenderer->setNoRender();
    $this->_helper->layout->disableLayout();

    print_r($_POST);
    $request = $this->getRequest();

    if ($request->isXmlHttpRequest())
    {
        $postdata = $request->getPost();
        print_r($postdata);
        if ($postdata)
        {
            $user = new Application_Model_DbTable_User();
            $user->updateSize($postdata);
        }
    }
}

I'm pretty sure that post data from a form is an array with the form elements' names as the keys. When looking at the dojo.xhrPost examples on the dojo campus web site (http://docs.dojocampus.org/dojo/xhrPost second one to be precise), it looks as if I can just send a string of data. How do I access this data from a Zend Controller Action?

I'm using ZF 1.10 and Dojo 1.4.2

Thanks for your help!

PS I'd try to ask on one of the related questions, but I cannot seem to comment.

A: 

After reading about http methods here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

I figured that I need to encode the data sent in a way that will be converted to an array by PHP. So here is the new bit of javascript:

function resizeText(multiplier)
{
    if (document.body.style.fontSize == "")
    {
        document.body.style.fontSize = "1.0em";
    }
    document.body.style.fontSize = parseFloat(document.body.style.fontSize) + (multiplier * 0.1) + "em";
    var rawdata = "uisize="+document.body.style.fontSize;
    var xhrArgs = {
                    url: "<?= $sizeurl; ?>",
                    postData: rawdata,
                    handleAs: "text"
                }
    //Call the asynchronous xhrPost
    dojo.xhrPost(xhrArgs);
}

The difference is I am now specifying a key pair and sending that. When using AJAX that could make forms overkill. So now my UI is resized and the size is stored with the user's profile. So the next page they request will use the size they set. Cool.

sims