Calling a PHP function through an HTML Link (no form)

13,600

Solution 1

You will need to use AJAX to accomplish this without leaving the page. Here is an example using jQuery and AJAX (this assumes you have already included the jQuery library):

First File:

<script language="javascript">

$(function(){
    $('#mylink').click(function(){
        $.get('/ajax/someurl', {linkText: $(this).text()}, function(resp){
           // handle response here
        }, 'json');
    });

});

</script>

<a href="#" id="mylink">This text will be passed along</a>

PHP File:

$text = $_REQUEST['linkText'];
// do something with $text here

Solution 2

You'll need to have a JS function which is triggered by an onclick event which then sends an AJAX request and returns false (so it won't be redirected to a new page in the browser). You can do the following in jQuery:

jQuery:

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

And in your page body:

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

In myfile.php:

You can add whatever function you want to execute when the visitor clicks the link. Example:

<?php
echo "Hey, this is some text!";
?>

That's a basic example. I hope this helps.

Solution 3

If you are familiar with jQuery, you could do the following, if you don't want the site to redirect but execute your function:

in your html head:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

the link:

<a href="#" onclick="$.get('ajax.php');">Execute function</a>

in ajax.php you put in your function to be executed.

Solution 4

Maybe something like this:

<a href="#" onclick="return sendText(this);"></a>
....
<script>
function sendText(e)
{
 $.ajax({
         url: '/your/url/',
         data: {text: $(e).html()},
         type: 'POST' 
        });
}
</script>

Solution 5

You can use query strings for this. For example if you link to this page:

example.php?text=hello

(Instead of putting a direct link, you can also send a ajax GET request to that URL)

Inside example.php, you can get the value 'hello' like this:

<?php
$text = $_GET['hello'];

Then call your function:

myfunction($text); 

Please make sure you sanitize and validate the value before passing it to the function. Depending on what you're doing inside that function, the outcome could be fatal!

This links might help:

Share:
13,600
pdt2383
Author by

pdt2383

Updated on August 01, 2022

Comments

  • pdt2383
    pdt2383 over 1 year

    I have a PHP Function that I would like to integrate into my (existing) web page. Further, I would like it to execute when the user clicks a link on the page. The function needs to accept the text of the link as an input argument.

    Everything I've researched for sending data to a PHP script seems to involve using forms to obtain user input. The page needs to accept no user input, just send the link-text to the function and execute that function.

    So I guess the question is two-part. First, how to execute a PHP script on link click. And second, how to pass page information to this function without the use of forms. I am open to the use of other technologies such as AJAX or JavaScript if necessary.

    EDIT:: Specifically what I am trying to do. I have an HTML output representing documentation of some source code. On this output is a series of links (referring to code constructs in the source code) that, upon being clicked, will call some python function installed on the web server (which leads me to think it needs called via PHP). The python function, however, needs the name present on the link as an input argument.

    Is there some sort of interaction I could achieve by having JavaScript gather the input and call the PHP function?

    Sorry for the vagueness, I am INCREDIBLY new to web development. If anything is unclear let me know.

  • tftd
    tftd almost 11 years
    although, technically this code is right, javascript does not mean jQuery :)
  • andy
    andy almost 11 years
    Why using inline event binding if you've got jQuery included? $(function(){$('a.my-link').bind('click', function(){ $.get('ajax.php'); });});
  • Daniel W.
    Daniel W. almost 11 years
    because it's easier to understand for a newbie and works just the same
  • drull
    drull almost 11 years
    of cource, but for ajax it is much better to use jquery, then own-made crutches
  • andy
    andy almost 11 years
    It does, but it's bad practice generally
  • Daniel W.
    Daniel W. almost 11 years
    Can you provide a link to an official site like w3c that says html attribute event handler are deprecated, or even removed from the html standards?