Zend Framework:在不离开网页的情况下执行脚本的超链接



我正在使用Zend Framework创建一个web应用程序。在某些页面中,我有一个链接,假设单击时在服务器上执行脚本。我通过将此链接与动作控制器关联来实现这一点。我想做的是,当我点击链接时,动作控制器中的代码会执行,但不会离开我点击链接的原始页面

如何做到这一点?

请帮忙。。。。。

您需要使用ajax,因为您希望在不离开页面的情况下执行操作:

例如,如果您使用的是jquery:

$("#myLink").click(function()
{
    $.ajax({
       type: "GET",
       url: "/my/action", //your action
       data: "item="+item, //a value that you might want to send to your action
       success: function(html, msg){
           //do something on success
       }
     }); 
});

php中有很多方法可以做到这一点,但您需要进行页面刷新。这是我用来保持在同一页面上的一种方法$this->_redirect($this->getRequest()->getRequestUri());会带你回到发出请求的同一页面,但它会导致页面刷新
_forward()将允许您在同一请求中执行另一个操作,因此它可能对您有一些用处
您想做的事情可以在没有ajax的情况下完成,但您必须忍受一定数量的页面刷新

动作

 public function indexAction() {
        //get form and pass to view
        $form = new Admin_Form_Station();
        $form->setAction('/admin/index');
        $form->setName('setStation');
        $this->view->station = $this->_session->stationName;
        $this->view->stationComment = $this->_session->stationComment;
        $this->view->form = $form;
        try {
            //get form values from request object
            if ($this->getRequest()->isPost()) {
                if ($form->isValid($this->getRequest()->getPost())) {
                    $data = (object)$form->getValues();
                    //set session variable 'station'
                    $this->_session->station = $data->station;
                    //assign station name and comment to session
                    $station = new Application_Model_DbTable_Station();
                    $currentStation = $station->getStation($this->_session->station);
                    $this->_session->stationName    = $currentStation->station;
                    $this->_session->stationComment = $currentStation->comment;
                    //assign array() of stations to session namespace
                    $stations = $station->fetchAllStation();
                    $this->_session->stations = $stations;
                    //assign array() of bidlocations to session namespace
                    $bidLocation  = new Application_Model_DbTable_BidLocation();
                    $bidLocations = $bidLocation->fetchAllBidLocation($this->_stationId);
                    $this->_session->bidLocations = $bidLocations;
                    //display the same page with properties set
                    $this->_redirect($this->getRequest()->getRequestUri());
                }
            }
        } catch (Zend_Exception $e) {
            //assign error to flash messenger...TODO not for production
            $this->_helper->flashMessenger->addMessage($e->getMessage());
            //refresh the page and display message
            $this->_redirect($this->getRequest()->getRequestUri());
        }
    }

并且在视图中

<?php if (!$this->station): ?>
    <div class="span-5 prepend-2">
        <?php echo $this->form ?>
    </div>
    <div class="span-10 prepend-2 last">
        <p style="font-size: 2em">Please select the Station you wish to perform Administration actions on.</p>
    </div>
<?php else: ?>
    <div class="span-19 last">
        <?php echo $this->render('_station.phtml') ?>
    </div>
<?php endif; ?>

我找到了一个似乎有效的解决方案
像往常一样在视图中使用链接,它会调用并执行一个操作。在您正在处理的操作中,重定向回您正在查看的页面。需要考虑的一件事是,任何临时数据都将丢失,因此持久化(会话)您需要保留的任何临时数据的策略将非常重要。我通常会定期使用Zend_Session_Namespace来持久化数据。上面的代码示例是我如何持久化所需数据的一个很好的例子
我在自己的应用程序中测试过这一点,只要数据仍然可用,页面就会刷新,内容或url不会发生明显变化。

相关内容

最新更新