2

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.

8
  • Your PHP script can read information from the URL without using a form; you can access it via $_GET, if you pass in an ampersand-separated list of field=value items' or you can get the whole thing in $_SERVER['QUERY_STRING'] Commented Jun 25, 2013 at 15:12
  • you can either use GET as andrewsi suggested or use AJAX and pass the info with POST. Commented Jun 25, 2013 at 15:12
  • @andrewsi you should write that as an answer because it's correct Commented Jun 25, 2013 at 15:13
  • @Naryl AJAX does not have to be POST to work, you can have AJAX with GET, PUT, DELETE, OPTIONS and whatever else. Also, GET and POST have two different semantic meanings which you should read about. After all, why do we have 2 of them when they're so similar? Commented Jun 25, 2013 at 15:14
  • The big question is whether you want the browser to remain on the same page after the action, or output some kind of result. Depending on that answer you will need to use either Ajax or one of the simpler solutions mentioned as answers here. Commented Jun 25, 2013 at 15:14

7 Answers 7

3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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.

4 Comments

Why using inline event binding if you've got jQuery included? $(function(){$('a.my-link').bind('click', function(){ $.get('ajax.php'); });});
because it's easier to understand for a newbie and works just the same
It does, but it's bad practice generally
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?
1

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>

2 Comments

although, technically this code is right, javascript does not mean jQuery :)
of cource, but for ajax it is much better to use jquery, then own-made crutches
1

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:

Comments

0

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']);
}

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.