简体   繁体   中英

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

<!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)

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? why the code is not executed?

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

Sometimes, you may need to call some JavaScript from within a link. 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.

<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; at the end.

OR

Use href="javascript:void(0)"

you are using jquery. $.get() will be something like below.

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

for further help check http://api.jquery.com/jQuery.get/

You can also use jQuery Load

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

When you are using Jquery should be :

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.

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

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

This div is used to display up.php result.

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

Look into prevent default http://api.jquery.com/event.preventDefault/ To stop the original link functionality working

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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