HREF to call a PHP function and pass a variable?

html, php

Solution

You can do this easily without using a framework. By default, anything that comes after a ? in a URL is a GET variable.

So for example, www.google.com/search.html?term=blah

Would go to www.google.com/search.html, and would pass the GET variable "term" with the value "blah".

Multiple variables can be separated with a &

So for example, www.google.com/search.html?term=blah&term2=cool

The GET method is independent of PHP, and is part of the HTTP specification.

PHP handles GET requests easily by automatically creating the superglobal variable $_GET[], where each array index is a GET variable name and the value of the array index is the value of the variable.

Here is some demo code to show how this works:

<?php
    //check if the get variable exists
    if (isset($_GET['search']))
    {
        search($_GET['search']);
    }

    function Search($res)
    {
        //real search code goes here
        echo $res;
    }


?>

<a href="?search=15">Search</a>

which will print out 15 because it is the value of search and my search dummy function just prints out any result it gets

Problem

Is it possible to create an `HREF` link that calls a PHP function and passes a variable along with it? ``` <?php function sample(){ foreach ($json_output->object ){ $name = "{$object->title}"; $id = "{$object->id}"; print "<a href='search($id)' >$name</a>"; } } function search($id){ //run a search via the id provide by the clicking of that particular name link } ?> ```

Original source