【问题标题】:Zend : create PHP namespace through JQuery executing errorZend:通过 JQuery 执行错误创建 PHP 命名空间
【发布时间】:2013-01-18 07:14:43
【问题描述】:

示例代码:

jQuery(function() {
    jQuery('#changePwYes').click(function(){
        <?php 
            $this->session = new Zend_Session_Namespace(Zend_Registry::get('config')->session->nameSpace);
            $this->session->showBox = "0";
        ?>
    });

});

此 php 脚本应仅在单击按钮时执行,但在加载时执行。

怎么办

【问题讨论】:

  • 是的。原因 php 在你的 jquery 被执行之前运行。
  • 动态需要使用ajax..
  • PHP 在您的服务器上运行。 JavaScript 在您客户的网络浏览器上运行。你不能像这样混合它们。

标签: php jquery ajax zend-framework


【解决方案1】:

在这种情况下,当您想从客户端在服务器端进行一些更改而不进行页面重定向时,我们有 AJAX

通过 AJAX 从您的点击事件中调用 Zend 控制器 操作:

jQuery(function() {
    jQuery('#changePwYes').click(function(){
        $.ajax({
         url: "myApp/public/index.php/controller-name/create-name-space/format/html",
         type: "POST",
         data:{mydata : 'test'}
         success: function(html){   
            alert('Done');    
         },
         error: function(jqXHR, textStatus, errorThrown){       
            alert('An error occurred);
         }
        });
    });
});

在您的控制器中,创建一个新的动作

public function createNameSpaceAction()
{       
    //Disable the layout rendering for the ajax request
    $this->_helper->layout->disableLayout();
    //Set no renderer in this case
    $this->_helper->viewRenderer->setNoRender(true);    

    //Retrieve dada if needed
    $myData = $_POST['mydata'];

    $session = new Zend_Session_Namespace(Zend_Registry::get('config')->session->nameSpace);
    $session->showBox = "0";    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多