Home  >  Article  >  Backend Development  >  Internship Summary 12: Examples of Ajax get function usage

Internship Summary 12: Examples of Ajax get function usage

WBOY
WBOYOriginal
2016-07-30 13:30:221128browse

It’s been a while since I blogged. It’s not because I’m slacking off, but I’ve recently been writing a large module for post article management. Since I didn’t learn much about php and zend, it took me about a week to implement the add, delete, modify, and check function. , I have recently been improving the module function, because the amount of code in it is too large, and I have to think about it in detail before writing a blog. This time I am writing about modifying the article status, using ajax. I never knew the benefits of ajax before. After using it specifically this time, I finally know a little bit about it. Next, let’s get to the point.

First post the ajax code:

<code><span>script</span>><span>
$(<span><span>function</span><span>()</span> {</span>    $(<span>".post-list-table .check"</span>).click(<span><span>function</span><span>()</span> {</span><span>var</span> post_id = $(<span>this</span>).parent().attr(<span>"alt"</span>);
        <span>var</span> status = $(<span>this</span>).attr(<span>"alt"</span>);
        $.get(<span>"/post/operate/"</span>+ post_id +<span>"/?status="</span>+ status, <span><span>function</span><span>(ret)</span> {</span>
            console.log(ret);
            <span>if</span>(ret[<span>1</span>] == <span>1</span>) {
                $(<span>"table tbody .status-"</span>+ post_id).html(<span>"Published"</span>);
            } <span>else</span><span>if</span>(ret[<span>1</span>] == -<span>1</span>) {
                $(<span>"table tbody .status-"</span>+ post_id).html(<span>"Rejected"</span>);
            } <span>else</span> {
                $(<span>"table tbody .status-"</span>+ post_id).html(<span>"Draft"</span>);
            }
        });
    });

    $(<span>".post-list-table .delete"</span>).click(<span><span>function</span><span>()</span> {</span><span>if</span>(confirm(<span>'确认删除?'</span>)) {
            <span>var</span> url = $(<span>this</span>).attr(<span>'url'</span>);

            $.getJSON(url, <span><span>function</span><span>(ret)</span> {</span>console.log(ret);
                <span>if</span>(ret[<span>0</span>] == <span>true</span>) {
                    $(<span>'.delete[url="'</span>+url+<span>'"]'</span>).parents(<span>'tr'</span>).remove();
                }
            });
        }
    });
})
</span><span><span>script</span>></span></code>

From the code, it can be seen that the two functions of changing status and deletion are implemented, which come from different actions. Next, post the action in the controller.

<code><span>public</span><span><span>function</span><span>operateAction</span><span>()</span>
    {</span><span>if</span>(!<span>$this</span>->userHasPermission(<span>'ADMIN'</span>, <span>'EDIT_REVIEW'</span>))
        {
            <span>return</span><span>$this</span>->requirePermission(<span>'ADMIN'</span>, <span>'EDIT_REVIEW'</span>);
        }
        <span>$ret</span> = <span>false</span>;
        <span>$request</span> = <span>$this</span>->getRequest();

        <span>$log_table</span> = <span>$this</span>->getPostLogTable();

        <span>$user_service</span> = <span>$this</span>->getServiceLocator()->get(<span>'UserService'</span>);
        <span>$curr_user</span> = <span>$user_service</span>->getCurrentUser();

        <span>$post_id</span> = <span>$this</span>->params()->fromRoute(<span>'id'</span>, <span>null</span>);

        <span>$post</span> = <span>$this</span>->getPostTable()->getPostById(<span>$post_id</span>);

        <span>$from_status</span> = <span>$post</span>[<span>'post_status'</span>];

        <span>$status</span> = <span>$request</span>->getQuery(<span>'status'</span>, <span>null</span>);
        <span>$log_row</span> = <span>array</span>();
        <span>if</span> (!is_null(<span>$status</span>)) {

            <span>if</span>(<span>$post</span>[<span>'post_status'</span>] != <span>$status</span>) {
                <span>$ret</span> = <span>$this</span>->getPostTable()->checkStatus(<span>$post</span>[<span>'id'</span>], (int)<span>$status</span>);
                <span>//var_dump($ret);exit();</span><span>if</span> (<span>$ret</span>) {
                    <span>$log_row</span>[<span>'post_id'</span>] = <span>$post_id</span>;
                    <span>$log_row</span>[<span>'user_id'</span>] = <span>$curr_user</span>->id;
                    <span>$log_row</span>[<span>'user_name'</span>] = <span>$curr_user</span>->username; 
                    <span>$log_row</span>[<span>'date'</span>] = date(<span>'y-m-d'</span>,time());
                    <span>$log_row</span>[<span>'from_status'</span>] = <span>$from_status</span>;
                    <span>$log_row</span>[<span>'to_status'</span>] = <span>$status</span>;

                    <span>$log_table</span>->addRows(<span>$log_row</span>);
                }
            }
            <span>$ret</span> = <span>true</span>;
        }
        <span>$jsonModel</span> = <span>new</span> JsonModel(<span>array</span>(<span>$post_id</span>, <span>$ret</span> ? (int)<span>$status</span> : <span>$ret</span>));
        <span>//var_dump($jsonModel);exit();</span><span>return</span><span>$jsonModel</span>;
    }

    <span>public</span><span><span>function</span><span>deletePostAction</span><span>()</span>{</span><span>if</span>(!<span>$this</span>->userHasPermission(<span>'ADMIN'</span>, <span>'VIEW_PRODUCT'</span>))
        {
            <span>return</span><span>$this</span>->requirePermission(<span>'ADMIN'</span>, <span>'VIEW_PRODUCT'</span>);
        }
        <span>$post_id</span> = (int) <span>$this</span>->params()->fromRoute(<span>'post_id'</span>, <span>0</span>);
        <span>$ret</span> = <span>false</span>;
        <span>if</span> (<span>$post_id</span>) {
            <span>$table</span> = <span>$this</span>->getPostTable();
            <span>$table</span>->deleteRowById(<span>$post_id</span>);
            <span>$this</span>->layout()->selectedTab = <span>'post-list'</span>;
            <span>$ret</span> = <span>true</span>;
        }

        <span>return</span><span>new</span> JsonModel(<span>array</span>(<span>$ret</span>));
    }</code>

I won’t write the routing settings for the two actions, and there is no specific phtml page, just to implement the function.

In the ajax code, through the .get(url,data) function, note that the data here refers to the array array of JsonModel returned by action or phtml, which is all the data returned. In fact, after click, the first parameter of get executes the action, obtains the data by the way, and then performs the operation according to the parameters, which is very convenient.

Post a picture, although I can’t see the effect:
If you click the green check, the status will change to Published. If you click the red cross, it will change to Rejected. If you click the red trash can, you will click Delete.
Internship Summary 12: Examples of Ajax get function usage

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above has introduced the internship summary twelve: examples of the use of the Ajax get function, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn