Code for jQuery ajax to implement the post like function

Source: Internet
Author: User

The code is simple, implemented by jQuery + php.

JQuery code:

The code is as follows: Copy code

JQuery (document). ready (function ($ ){
$ (". Zan"). click (function (e ){
Var $ I = $ (". zan I "), $ B =$ (" <B> "). text ("+ 1"), n = parseInt ($ I. text ());
$ B .css ({
"Bottom": 0,
"Z-index": 999,
});
$ I. text (n + 1 );
$ (". Zan"). append ($ B );
$ B. animate ({"bottom": 100, "opacity": 0 }, 1000, function () {$ B. remove ();});
Var d = setInterval (function (){
ClearInterval (d );
If ($ (". zan B"). length = 1 ){
$. Post ("", {zan: $ I. text ()})
}
}, 1000)
E. stopPropagation ();
});
});

Php code:

The code is as follows: Copy code

<? Php
$ Path = "zan.txt ";
If (isset ($ _ POST ['zany']) {
$ Num = (int) $ _ POST ['zany'];
$ Save = fopen ($ path, "w ");
Fwrite ($ save, $ num );
Fclose ($ save );
Echo "good ";
Exit ();
$ Zan = file_exists ($ path )? Intval (file_get_contents ($ path): 0;
}
?>

Html code:

The code is as follows: Copy code

<Div class = "main">
<Div class = "zan"> <em> The audience gave <I> <? Php echo $ zan;?> </I> likes </em> </div>
</Div>

With appropriate css styles:

The code is as follows: Copy code

. Main {position: relative; font-size: 10pt; line-height: 18px; margin: 40px auto; width: 800px; height: 170px;-webkit-user-select: none; -moz-user-select: none;-ms-user-select: none;-o-user-select: none ;}
. Zan {position: absolute; top: 20%; left: 45%; width: 160px; height: 110px; background: url ("http://www.111cn.net/zan.jpg") center no-repeat; cursor: pointer; opacity: 0.85 ;}
. Zan: active {opacity: 1 ;}
. Zan em {position: absolute; color: #333; font-style: normal; bottom:-15px; width: 100%; text-align: center ;}
. Zan I {font-style: normal; color: # E94F06 ;}
. Zan B {position: absolute; color: # E94F06; font-style: normal; bottom:-15px; width: 100%; text-align: center ;}

That's it. It's easy!

The complete code is as follows:

The code is as follows: Copy code

<? Php
$ Path = "zan.txt ";
If (isset ($ _ POST ['zany']) {
$ Num = (int) $ _ POST ['zany'];
$ Save = fopen ($ path, "w ");
Fwrite ($ save, $ num );
Fclose ($ save );
Echo "good ";
Exit ();
}
$ Zan = file_exists ($ path )? Intval (file_get_contents ($ path): 0;
?>
<! Doctype html>
<Html>
<Head>
<Meta charset = "utf-8">
<Title> I want to like it </title>
<Style>
. Main {position: relative; font-size: 10pt; line-height: 18px; margin: 40px auto; width: 800px; height: 170px;-webkit-user-select: none; -moz-user-select: none;-ms-user-select: none;-o-user-select: none ;}
. Zan {position: absolute; top: 20%; left: 45%; width: 160px; height: 110px; background: url ("http://www.111cn.net/zan.jpg") center no-repeat; cursor: pointer; opacity: 0.85 ;}
. Zan: active {opacity: 1 ;}
. Zan em {position: absolute; color: #333; font-style: normal; bottom:-15px; width: 100%; text-align: center ;}
. Zan I {font-style: normal; color: # E94F06 ;}
. Zan B {position: absolute; color: # E94F06; font-style: normal; bottom:-15px; width: 100%; text-align: center ;}
</Style>
</Head>
<Body>
<Div class = "main">
<Div class = "zan"> <em> The audience gave <I> <? Php echo $ zan;?> </I> likes </em> </div>
</Div>
<Script src = "http://cdn.bootcss.com/jquery/2.1.0/jquery.min.js"> </script>
<Script>
JQuery (document). ready (function ($ ){
$ (". Zan"). click (function (e ){
Var $ I = $ (". zan I "), $ B =$ (" <B> "). text ("+ 1"), n = parseInt ($ I. text ());
$ B .css ({
"Bottom": 0,
"Z-index": 999,
});
$ I. text (n + 1 );
$ (". Zan"). append ($ B );
$ B. animate ({"bottom": 100, "opacity": 0 }, 1000, function () {$ B. remove ();});
Var d = setInterval (function (){
ClearInterval (d );
If ($ (". zan B"). length = 1 ){
$. Post ("", {zan: $ I. text ()})
}
}, 1000)
E. stopPropagation ();
});
});
</Script>
</Body>
</Html>

There is no upper limit to likes in the title. So, let's talk about the following tips:

The code is as follows: Copy code

/* Angry likes */
JQuery. noConflict ();
Function zan (){
SetInterval (function (){
JQuery (". zan"). click ();
Zan ();
}, 600)
}
Zan ();

Of course, if you use jiasule anti-CC (matching the green onion), too many requests will be blocked! Unless you cancel the POST in jQuery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.