duanlu8613 2018-12-10 21:57
浏览 52

PHP - Zend3,Jquery,Ajax(帖子)

I have built a website using php and the zend framework. In one of the pages I have a zend form and a table. The user can fill in the form, click the search button(page refresh occurs) and then get the corresponding results in the table.

What I am trying to do is to implement the same functionality using Ajax so the page won't have to refresh or ask for re-submission when reloaded.

From my controller I pass the data I want to display to view.phtml. When the page first opens all the data from database gets displayed in the table. Somehow after the user clicks search :

  • the ajax post data should be retrieved in the controller
  • compared to the rest of the data to see if there are any matches
  • return the data matched

    public function searchAction(): ViewModel
    {
        $persons = $this->personsService->getAllPersons();
        $form = $this->personsForm;
    
    if ($this->getRequest()->isPost()) {
        $formData = $this->params()->fromPost();
        $form->setData($formData);
    
        if ($form->isValid()) {
            $validFilteredData = $form->getData();
            $persons = $this->personsService->getPersonsAfterSearch($validFilteredData);
        }
    }
    
    return new ViewModel([
        'persons' => $persons,
        'form' => $form,
    ]);
    }
    

I would like any suggestions on how to implement ajax since I am a beginner in web development and I don't experience working with ajax.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongyange1101 2018-12-11 10:25
    关注

    Before you do this:

    return new ViewModel([
        'persons' => $persons,
        'form' => $form,
    ]);
    

    Add this:

    if ($this->getRequest()->isXmlHttpRequest()) {
        return new \Zend\View\Model\JsonModel(
            [
                'persons' => $persons,
                'form'    => $form,
            ]
        );
    }
    

    Note: you've tagged "zend-framework" but mentioned "zend3". Above solution works for ZF2 and ZF3, don't know about ZF1.


    Update due to comments:

    Full function would be:

    public function searchAction() : ViewModel
    {
        $persons = $this->personsService->getAllPersons();
        $form = $this->personsForm;
    
        if ($this->getRequest()->isPost()) {
            $formData = $this->params()->fromPost();
            $form->setData($formData);
    
            if ($form->isValid()) {
                $validFilteredData = $form->getData();
                $persons = $this->personsService->getPersonsAfterSearch($validFilteredData);
            }
        }
    
        $data = [
            'persons' => $persons,
            'form'    => $form,
        ];
    
        // AJAX response
        if ($this->getRequest()->isXmlHttpRequest()) {
            return new \Zend\View\Model\JsonModel($data);
        }
    
        return $data; // No need to return "new ViewModel", handled via ZF magic
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号