简体   繁体   English

从链接运行php代码而无需重定向

[英]running a php code from a link without redirection

I want to execute a php code when a user click on a given link , i've tried the following code 当用户单击给定链接时,我想执行一个php代码,我已经尝试了以下代码

<!DOCTYPE html>
<html>
<head>

 <script type="text/javascript" src="jquery.min.js"></script>
 <script type="text/javascript">
 function doSomething() {
 $.get("up.php");
 return false;
 }
</script>


  </head>

  <body>

  <center>
  <a href="#" onclick="doSomething();">Click Me!</a>


   </center>

   </body>
  </html>

where up.php is a php code that implement android GCM mechanism similer to the one in the link : GCM with PHP (Google Cloud Messaging) 其中up.php是实现类似于该链接中的android GCM机制的php代码: 带PHP的GCM(Google云消息传递)

but unfortunately when the link is clicked nothing happens , the php code not executed ,and only the url of the page change form *****.com/test.html to *****.com/test.html# , and by the way I've tested the php code and its works fine so it is not a problem in php code , so what i have missed here? 但是不幸的是,当单击链接时,什么也没有发生,没有执行php代码,只有页面的URL形式从*****。com / test.html更改为*****。com / test.html#,顺便说一下,我已经测试了php代码,它的工作原理很好,所以这不是php代码中的问题,所以我在这里错过了什么? why the code is not executed? 为什么代码没有执行?

我找到了解决方案,我的代码中可能未包含jquery库的问题

Sometimes, you may need to call some JavaScript from within a link. 有时,您可能需要从链接中调用一些JavaScript。 Normally, when user click on a link, the browser loads a new page (or refreshes the same page). 通常,当用户单击链接时,浏览器将加载新页面(或刷新同一页面)。

This might not always be desirable. 这可能并不总是令人满意的。 For example, you might only want to dynamically update a form field when the user clicks a link. 例如,您可能只想在用户单击链接时动态更新表单字段。

To prevent the load from refreshing, you could use the JavaScript void() function and pass a parameter of 0 (zero) as below. 为了防止负载刷新,可以使用JavaScript void()函数并按如下所示传递参数0(零)。

<a href="JavaScript:void(0);" onclick="return doSomething(); " />Click Me!</a>

Now if you want to use href="#", make sure onclick always contains return false; 现在,如果要使用href =“#”,请确保onclick始终包含return false; at the end. 在末尾。

OR 要么

Use href="javascript:void(0)" 使用href="javascript:void(0)"

you are using jquery. 您正在使用jquery。 $.get() will be something like below. $ .get()将如下所示。

$.get('ajax/test.html', function(data) {
 $('.result').html(data);
 alert('Load was performed.');
});

for further help check http://api.jquery.com/jQuery.get/ 如需更多帮助,请访问http://api.jquery.com/jQuery.get/

You can also use jQuery Load 您还可以使用jQuery Load

function doSomething() {
$('#result').load('up.php', function() {
  alert('Load was performed.');
});
}

When you are using Jquery should be : 当您使用Jquery时应为:

function doSomething(){
    $.get('up.php', function(data) {
        $('.data').html(data);
    });
}

Link 链接

To stop page refreshing, you can use the javascript void() function and pass parameter of 0. 要停止页面刷新,可以使用javascript void()函数并传递参数0。

<a href="javascript:void(0);" onclick="return doSomething();">Click Me!</a>

<div class="data"></div>

This div is used to display up.php result. 该div用于显示up.php结果。

Ok start of by using fire bug and watch the console and network panels Ensure the browser is trying to get something and not returning a 404 or a php error on the get 首先,使用Fire Bug,观察控制台和网络面板,确保浏览器尝试获取内容,并且在获取时未返回404或php错误

Look into prevent default http://api.jquery.com/event.preventDefault/ To stop the original link functionality working 调查防止默认http://api.jquery.com/event.preventDefault/停止原始链接功能的工作

If you still want them to move to the link use a window location or... 如果您仍然希望他们移动到链接,请使用窗口位置或...

Look into bootstrap link buttons 查看引导链接按钮

http://twitter.github.com/bootstrap/base-css.html#buttons ( the link button ) http://twitter.github.com/bootstrap/base-css.html#buttons (链接按钮)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM