{"id":1717,"date":"2022-08-30T15:18:51","date_gmt":"2022-08-30T15:18:51","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/29\/how-to-redirect-zend-login-form-in-a-fancbox-iframe-with-php-without-any-js-only-on-successful-login-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:18:51","modified_gmt":"2022-08-30T15:18:51","slug":"how-to-redirect-zend-login-form-in-a-fancbox-iframe-with-php-without-any-js-only-on-successful-login-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-redirect-zend-login-form-in-a-fancbox-iframe-with-php-without-any-js-only-on-successful-login-collection-of-common-programming-errors\/","title":{"rendered":"How to redirect Zend login form in a fancbox iframe with PHP (without any JS) only on ***successful*** login-Collection of common programming errors"},"content":{"rendered":"<p>Using only PHP (i.e. no JS), how do you redirect a Zend form in a fancybox iframe to its parent page, only if the form was successful? For instance, I set setAttrib(&#8220;target&#8221;,&#8221;_parent&#8221;) in the Zend form, but it forces the submit action to redirect on the iframe, and not the parent page. I want it to return to the parent page only if the submit action is unsuccessful. If login is not successful, it stays on the fancybox iframe successfully displaying error message. I am using Zend Framework. Here are some code:<\/p>\n<p>Login.php<\/p>\n<pre><code>class Application_Form_Login extends Zend_Form\n{\n\n    public function init()\n    {\n        $this-&gt;setAction('\/auth\/login')\n             -&gt;setAttrib('id', 'login_form')\n             -&gt;setAttrib('target', '_parent')\n             -&gt;setName('login_form')\n                     -&gt;setMethod('post');\n\n    -----&gt;added all elements here\n    }\n\n}\n<\/code><\/pre>\n<p>loginAction (inside authController.php)<\/p>\n<pre><code>    public function loginAction()\n    {\n\n        $request = $this-&gt;getRequest();\n        $form = new Application_Form_Login();\n        \/\/$form-&gt;setAttrib('target','_parent');\n        \/\/$this-&gt;view-&gt;login_form = $form;\n\n        if($request-&gt;isPost()){\n            if($form-&gt;isValid($request-&gt;getPost())){\n                $data = $form-&gt;getValues();\n\n                if ($this-&gt;_processLogin($data)) {\n                    \/\/ We're authenticated! Redirect to the home page\n                    $auth = Zend_Auth::getInstance();\n                    $id = $auth-&gt;getIdentity()-&gt;id;\n                        \/\/this is where I need some logic to redirect on parent page only if successful login \n                    $this-&gt;_redirect('\/user\/'.$id.'\/home');  \n                } else {\n                    $form-&gt;setAttrib('target','_self');\n                    $this-&gt;view-&gt;errorMessage = \"Invalid credentials. Try again\";\n                    $form = new Application_Form_Login();\n                    $this-&gt;view-&gt;login_form = $form;\n                }         \n            }else{\n                    $form-&gt;setAttrib('target','_self');\n                    $this-&gt;view-&gt;errorMessage = \"Can't be empty. Please try again.\";\n                    $form = new Application_Form_Login();\n                    $this-&gt;view-&gt;login_form = $form;\n            }\n        }else{\n        }\n\n        $this-&gt;view-&gt;login_form = $form;\n    } \n<\/code><\/pre>\n<ol>\n<li>\n<p>Here&#8217;s what I ended up doing:<\/p>\n<pre><code> regular stuff here, elements, etc\n\n\n\n\n\n$(document).ready(function() {\n\n    $('#loginbtn').live('click',function(event){\n\n        var em = $(\"#useremail\").val();\n        var pwd = $(\"#userpassword\").val();\n\n        $.ajax({\n            type: \"POST\",\n            dataType: 'json',\n            url: \"\/auth\/login\",\n            data: { email: em, password: pwd },\n            success: function(result){\n                if(result.msg == \"success\"){\n                    window.location = \"\/user\/\" + result.id + \"\/home\";\n                }else{\n                    $(\".errormsg\").html(result.msg);\n                }\n            },\n            error: function(jqXHR, exception) {\n                if (jqXHR.status === 0) {\n                    alert('Not connect.\\n Verify Network.');\n                } else if (jqXHR.status == 404) {\n                    alert('Requested page not found. [404]');\n                } else if (jqXHR.status == 500) {\n                    alert('Internal Server Error [500].');\n                } else if (exception === 'parsererror') {\n                    alert('Requested JSON parse failed.');\n                } else if (exception === 'timeout') {\n                    alert('Time out error.');\n                } else if (exception === 'abort') {\n                    alert('Ajax request aborted.');\n                } else {\n                    alert('Uncaught Error.\\n' + jqXHR.responseText);\n                }\n            }       \n        });\n\n        return false;\n    });\n});\n<\/code><\/pre>\n<p>And in my controller action:<\/p>\n<pre><code>public function loginAction()\n<\/code><\/pre>\n<p>{<\/p>\n<pre><code>    $this-&gt;_helper-&gt;layout-&gt;disableLayout();\n        $this-&gt;_helper-&gt;viewRenderer-&gt;setNoRender(TRUE); \/\/ suppress auto-rendering and displaying a view\n    $data['email'] = $_POST['email'];\n    $data['password'] = $_POST['password'];\n\n    if ($this-&gt;_processLogin($data)) {\n            \/\/ We're authenticated!\n        $auth = Zend_Auth::getInstance();\n        $id = $auth-&gt;getIdentity()-&gt;id;\n        echo Zend_Json::encode(array('msg' =&gt; 'success', 'id' =&gt; $id));\n    } else {\n                  $this-&gt;_helper-&gt;json(array('msg' =&gt; 'Invalid credentials. Try again'));\n        } \n\n    }\n<\/code><\/pre>\n<\/li>\n<li>\n<p>I think you can&#8217;t do such thing.<\/p>\n<p>The javascript would reference the parent page in that case. The server side has no way to set in the headers those instructions.<\/p>\n<p>The best you can do is to redirect the page to another one with the javascript instruction referencing the parent page.<\/p>\n<p>Using the target attribute it will redirect in any case.<\/p>\n<p>Example:<\/p>\n<p><strong>AuthController.php<\/strong><\/p>\n<pre><code>public function loginAction() {\n    \/\/ ...\n    if(\/* is password correct *\/) {\n        \/\/...\n        \/\/ After a successfull login, just render the view\n        $this-&gt;render('auth\/redirect.phtml');\n    }\n    \/\/ ...\n}\n<\/code><\/pre>\n<p><strong>redirect.phtml<\/strong><\/p>\n<pre><code>\n\n    parent.window.location.href = '';\n\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-29 06:17:23. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Using only PHP (i.e. no JS), how do you redirect a Zend form in a fancybox iframe to its parent page, only if the form was successful? For instance, I set setAttrib(&#8220;target&#8221;,&#8221;_parent&#8221;) in the Zend form, but it forces the submit action to redirect on the iframe, and not the parent page. I want it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,8],"tags":[],"class_list":["post-1717","post","type-post","status-publish","format-standard","hentry","category-uncategorized","category-zend-framework"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1717"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1717\/revisions"}],"predecessor-version":[{"id":8896,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1717\/revisions\/8896"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}