: This article mainly introduces the practice example 12: Ajax get function examples. if you are interested in the PHP Tutorial, please refer to it. I haven't been writing a blog for a long time. I'm not slack, but I am writing a large module for Post article management recently. it took about a week because php and zend didn't learn much, added, deleted, modified, and queried functions. recently, the module function has been improved. because the amount of code in the module is too large, you have to think about it before you can write a blog. this time, you write a blog to modify the article status, after ajax is used, I have never known about the benefits of ajax. this time, I have learned some details about it. Next, let's get started.
First, paste the ajax code:
From the code, we can see that the two functions are changed and deleted, respectively, from different actions, and then paste the action in the controller.
publicfunctionoperateAction() {if(!$this->userHasPermission('ADMIN', 'EDIT_REVIEW')) { return$this->requirePermission('ADMIN', 'EDIT_REVIEW'); } $ret = false; $request = $this->getRequest(); $log_table = $this->getPostLogTable(); $user_service = $this->getServiceLocator()->get('UserService'); $curr_user = $user_service->getCurrentUser(); $post_id = $this->params()->fromRoute('id', null); $post = $this->getPostTable()->getPostById($post_id); $from_status = $post['post_status']; $status = $request->getQuery('status', null); $log_row = array(); if (!is_null($status)) { if($post['post_status'] != $status) { $ret = $this->getPostTable()->checkStatus($post['id'], (int)$status); //var_dump($ret);exit();if ($ret) { $log_row['post_id'] = $post_id; $log_row['user_id'] = $curr_user->id; $log_row['user_name'] = $curr_user->username; $log_row['date'] = date('y-m-d',time()); $log_row['from_status'] = $from_status; $log_row['to_status'] = $status; $log_table->addRows($log_row); } } $ret = true; } $jsonModel = new JsonModel(array($post_id, $ret ? (int)$status : $ret)); //var_dump($jsonModel);exit();return$jsonModel; } publicfunctiondeletePostAction(){if(!$this->userHasPermission('ADMIN', 'VIEW_PRODUCT')) { return$this->requirePermission('ADMIN', 'VIEW_PRODUCT'); } $post_id = (int) $this->params()->fromRoute('post_id', 0); $ret = false; if ($post_id) { $table = $this->getPostTable(); $table->deleteRowById($post_id); $this->layout()->selectedTab = 'post-list'; $ret = true; } returnnew JsonModel(array($ret)); }
The routing settings of the two actions will not be written, and there is no specific phtml web page, just implement the function.
In ajax code, through the. get (url, data) function, note that data here refers to the array of JsonModel returned by action or phtml, which is all the returned data. In fact, after clicking, the first get parameter executes the action, obtains the data by the way, and then executes the operation according to the parameter, which is very convenient.
Paste it one, although it does not show the effect:
If you click the green check box, the status will change to Published. click the Red Cross to change to Rejected, and click delete in the red bin.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above introduces the practice Summary 12: Ajax get function examples, including some content, hope to be helpful to friends who are interested in PHP tutorials.