如何做一个onclick AJAX调用到PHP文件,使用jQuery? [英] How to do an onclick ajax call to a php file, with jquery?

查看:258
本文介绍了如何做一个onclick AJAX调用到PHP文件,使用jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接,链接到domain.com,当一个人点击,我希望它做一个Ajax调用counter.php和后2变量,所以它可以增加1到该链接的意见。

I have a link, which links to domain.com , when a person clicks, I want it to do an ajax call to counter.php and post 2 variables to it, so it can add 1 to the views for that link.

我有一个链接:

<a href="http://www.domain.com" onClick="addHit('12345', '1')" rel="nofollow" target="_blank">Link Title</a>

我将如何做到这一点使用jQuery?

How would I do this with jquery?

编辑:

我想是这样的。

function addHit(str, partNumber){
$.get("counter.php", { version: str, part: partNumber })                
}

这似乎工作,但在萤火虫,请求无法完成......只是做了工作...动画。 counter.php回声出来的时候对其做(犯规需要的地方显示出来)一些文本。

It seems to work, but in firebug, the request never completes... it just does that "working..." animation. counter.php echos out some text when its done (doesnt need to show up anywhere).

推荐答案

从jQuery的文档: HTTP://api.jquery .COM / jQuery.ajax /

From the jQuery documentation: http://api.jquery.com/jQuery.ajax/

function addHit(data1, data2)
{
    $.ajax({
       type: "POST",
       url: "http://domain.com/counter.php",
       data: "var1=data1&var2=data2",
       success: function(msg){
         alert( "Data Saved: " + msg ); //Anything you want
       }
     });
}

这篇关于如何做一个onclick AJAX调用到PHP文件,使用jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆