How to Run PHP Code When a User Clicks on a Link

How do I run PHP code when a user clicks on a link?

Yeah, you'd need to have a javascript function triggered by an onclick that does an AJAX load of a page and then returns false, that way they won't be redirected in the browser. You could use the following in jQuery, if that's acceptable for your project:

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

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

You could also do a post-back if you need to use form values (use the $.post() method).

Execute PHP code by clicking a link

I assume your current URL is index.php

HTML

<a style='text-align: right;cursor:pointer;' href="index.php?reset=true" name="reset" class='system list-group-item'>

PHP

<?php
if (isset($_GET['reset'])) {
myFunction();
}
function myFunction() {
//do something
}
?>

Call a php function when an html link is clicked

Do it like this:

<a href="?sendcode=true">Didn't get a code?</a>
<?php
if(isset($_GET['sendcode'])){
sendCode();
}
function sendCode(){
//code
}
?>

run php script onclick link with javascript

You could use ajax to make a call to your php.script whenever you click on an element.

$('.element_you_click').on('click', function(){
jQuery.ajax({
url : 'path/to/your/script.php',
success: function(response){
#whatever you want to do after the script
}
});
});

Hope this helps you!

EDIT:

If you want to send variables to your script on this call just add these lines:

data:{"variable1" : value1, "variable2" : value2},
type: "GET", #or POST

and then retrieve the data on your script with $_GET['variable1'] and $_GET['variable2'].

How to know when a link is clicked in php

You can't. PHP is a preprocessor on the server side and doesn't know anything about what happens on the client side (the browser).

What you can do is create either an AJAX request that will call a PHP script on your server whenever the user clicks the link, or point the link at a PHP script directly.

Is there a way to add PHP code to a clickable link?

Link to a PHP function, run your code, last line of the function would be a header line.
header(Location: 'http://whateverlink.com');

Really no different than submitting a PHP form

Run PHP code when user clicks link and pass variables

use something like this in you page with link

Some text

in the same page put this somewhere on top

<script language='javascript'>
$(function(){
$('.myClass').click(function(){
var data1 = 'someString';
var data2 = 5;//some integer
var data3 = "<?php echo $somephpVariable?>";

$.ajax({

url : "phpfile.php (where you want to pass datas or run some php code)",
data: "d1="+data1+"&d2="+data2+"&d3="+data3,
type : "post",//can be get or post
success: function(){
alert('success');//do something
}

});
return false;
});
});

</script>

on the url mentioned in url: in ajax submission
you can fetch those datas passed
for examlple

<?php
$data1 =$_POST['d1'];
$data2 =$_POST['d2'];
$data3 =$_POST['d3'];
//now you can perform actions as you wish
?>

hope that helps



Related Topics



Leave a reply



Submit