通过HTML链接调用PHP函数(无表格)

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.

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

PHP is a server side language i.e. it doesn't run in the web browser.

If you want a function in the browser to operate on clicking a link you are probably talking about doing some Javascript.

You can use the Javascript to find the text value contained in the link node and send that to the server, then have your PHP script process it.

Here's an overly simplistic example of what you're trying to do..

Your link:

<a href="somefile.php?action=Some+Action">Some Action</a>

Your PHP file:

<?php

if (isset($_GET['action']))
{
    // make sure to validate your input here!
    some_function($_GET['action']);
}

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.

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>

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:

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.