1

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?

Community
  • 1
  • 1
OpEtMaR
  • 231
  • 4
  • 17
  • I've tried all the solutions below but non of them works for me , any idea? – OpEtMaR Feb 13 '13 at 12:08
  • are you sure, that up.php does not get executed? try writing something in a logfile when executing up.php. The way you wrote it, up.php may be executed, but you don't do anything with the result of up.php... – cypherabe Feb 13 '13 at 13:54

6 Answers6

1

i found the solution , the problem that the jquery library is not included probably in my code

OpEtMaR
  • 231
  • 4
  • 17
0

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

Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
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/

Ripa Saha
  • 2,532
  • 6
  • 27
  • 51
0

You can also use jQuery Load

function doSomething() {
$('#result').load('up.php', function() {
  alert('Load was performed.');
});
}
Surinder ツ
  • 1,778
  • 3
  • 16
  • 27
0

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.

Devang Rathod
  • 6,650
  • 2
  • 23
  • 32
0

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 )

Mark Jones
  • 115
  • 8