在onClick上执行php文件

Jon*_*son 4 php ajax jquery onclick

我目前有一个php文件执行:

<a href="test.php?foo=boo">test</a>
Run Code Online (Sandbox Code Playgroud)

我宁愿不必加载新页面并在onClick上执行.

有谁知道一个简单的方法来做到这一点?

我添加了一个更完整的示例,包括建议的ajax.我仍然无法让它工作.

<script type="text/javascript">
    $('li').click(function() {
    $.ajax({ type: "GET", url: "test.php", data: { foo: 'boo' }, success: function(data){
          // use this if you want to process the returned data
         alert('complete, returned:' + data);
    }});
    });
</script>
</head>
<body>
    <div id="header">
    <h1>Title</h1>
    </div>      
    <ul>
        <li><a href="#">Test</a></li>
    </ul>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Oli*_*cal 5

是的,正如您的标签所说.您需要使用AJAX.

例如:

$.ajax({
    type: "GET",
    url: "test.php",
    data: "foo=boo"
});
Run Code Online (Sandbox Code Playgroud)

然后你只需要在点击功能中坚持下去.

$('a').click(function() {
    // The previous function here...
});
Run Code Online (Sandbox Code Playgroud)

编辑:将方法更改为GET.


oez*_*ezi 5

你可以使用jquery并做这样的事情:

$.ajax({ url: "test.php", data: { foo: 'boo' }, success: function(data){
      // use this if you want to process the returned data
      // alert('complete, returned:' + data);
    }});
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请查看jquery文档