tags:

views:

27

answers:

1

How to return the results from the submission of a form to an iframe in cakephp?

Another question is, how to return some message to iframe for every iteration over the loop of a controller action. lets say it iterates for 10 times, so after every iteration, it will immediately show message like"iteration 1 completed" and so on... in the iframe....

A: 

I think you need to look at an AJAX approach to this. You can use Javascript to poke data into the iframe, but iterative interaction between the view and the controller is best done with an AJAX connection.

This is a quick and dirty approach I took. At the end of the controller action:

    App::import('Helper', 'Javascript');
    $javascript = new JavascriptHelper();

    echo($javascript->object($returnVals)); // allows passing of array
    exit(1);

In the view, you need to capture the returned value and parse using JSON.parse:

<snip-->
        if(xmlHttp)
        {
            xmlHttp.open("POST",actionURL,true);
            xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            xmlHttp.onreadystatechange = function()
            {
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
        {
            doAction(xmlHttp.responseText,zone);
        }
<--snip>
    function doAction(props,zone)
    {
        var newProps = JSON.parse(props);//needs to be parsed into a JS object.
        //other stuff here
    }
Leo
Thank leo...... I know I will need to use ajax....... but I just want to know, how to do it, let say i submit a form, and it runs a controller action, so now, how wud i send a $result to the iframe from this controller action, and how to fetch this $result into the iframe......
tecks
Without knowing the details of what exactly you're trying to do, it's difficult to say. I guess if you MUST use an iframe, then you need to maintain a handle to it and poke the appropriate info into it using javascript. I can't help thinking, though, that you're making things difficult for yourself.
Leo
I've edited my answer to include some code snippets.
Leo
Thanks, i will try...
tecks